如何将C#实现的Custom Identity Usermanager转换为F#代码
以下是和你提供的C#代码完全等效的F#实现,逻辑、依赖结构和原代码完全对齐:
open Microsoft.AspNetCore.Identity open Microsoft.Extensions.Options open System open System.Collections.Generic open Microsoft.Extensions.Logging open System.Threading.Tasks type ApplicationUserManager( store: IUserStore<ApplicationUser>, optionsAccessor: IOptions<IdentityOptions>, passwordHasher: IPasswordHasher<ApplicationUser>, userValidators: IEnumerable<IUserValidator<ApplicationUser>>, passwordValidators: IEnumerable<IPasswordValidator<ApplicationUser>>, keyNormalizer: ILookupNormalizer, errors: IdentityErrorDescriber, services: IServiceProvider, logger: ILogger<UserManager<ApplicationUser>> ) inherit UserManager<ApplicationUser>(store, optionsAccessor, passwordHasher, userValidators, passwordValidators, keyNormalizer, errors, services, logger) override _.ResetPasswordAsync(user: ApplicationUser, token: string, newPassword: string) = task { let! result = base.ResetPasswordAsync(user, token, newPassword) if result.Succeeded then user.ChangePasswordDate <- DateTime.Now.AddYears(1) user.ChangePassword <- false do! base.UpdateAsync(user) :> Task return result } :> Task<IdentityResult>
注意事项
- 代码默认你已经提前定义了
ApplicationUser类型,且包含ChangePasswordDate(DateTime类型)和ChangePassword(bool类型)两个可写属性 - F# 5及以上版本内置支持
task计算表达式,和C#的async/await行为完全一致;更低版本可以安装TaskBuilder.fsNuGet包实现相同效果 - 依赖注入的注册逻辑和C#版本完全一致,不需要额外调整
内容的提问来源于stack exchange,提问作者Glenn h
相关产品推荐
相关产品推荐

