You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将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.fs NuGet包实现相同效果
  • 依赖注入的注册逻辑和C#版本完全一致,不需要额外调整

内容的提问来源于stack exchange,提问作者Glenn h

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 10:06:04