从 C# 到 F# 的自定义身份用户管理器
Custom Identity Usermanager from C# to F#
我们目前有以下代码
public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(IUserStore<ApplicationUser> store, IOptions<IdentityOptions> optionsAccessor, IPasswordHasher<ApplicationUser> passwordHasher, IEnumerable<IUserValidator<ApplicationUser>> userValidators, IEnumerable<IPasswordValidator<ApplicationUser>> passwordValidators, ILookupNormalizer keyNormalizer, IdentityErrorDescriber errors, IServiceProvider services, ILogger<UserManager<ApplicationUser>> logger) : base(store, optionsAccessor, passwordHasher, userValidators, passwordValidators, keyNormalizer, errors, services, logger)
{
}
public override async Task<IdentityResult> ResetPasswordAsync(ApplicationUser user, string token, string newPassword)
{
var result = await base.ResetPasswordAsync(user, token, newPassword);
if (result.Succeeded)
{
user.ChangePasswordDate = DateTime.Now.AddYears(1);
user.ChangePassword = false;
await base.UpdateAsync(user);
}
return result;
}
}
我如何将其转换为 f#?
如有任何帮助,我们将不胜感激
你好,
格伦
如果您使用的是 F# 6,则可以使用新的 task builder,如下所示:
open Microsoft.AspNetCore.Identity
type ApplicationUserManager(store, optionsAccessor, passwordHasher, userValidators, passwordValidators, keyNormalizer, errors, services, logger) =
inherit UserManager<ApplicationUser>(store, optionsAccessor, passwordHasher, userValidators, passwordValidators, keyNormalizer, errors, services, logger)
member private _.BaseResetPasswordAsync(user, token, newPassword) =
base.ResetPasswordAsync(user, token, newPassword)
override this.ResetPasswordAsync(user, token, newPassword) =
task {
let! result = this.BaseResetPasswordAsync(user, token, newPassword)
if result.Succeeded then
user.ChangePasswordDate <- DateTime.Now.AddYears(1)
user.ChangePassword <- false
let! _result = this.UpdateAsync(user)
ignore _result
return result;
}
需要私有 Base
方法,因为 .
请注意,您在 C# 代码中忽略了 UpdateAsync
的结果。我在这里做过同样的事情,但我不知道这样做是否明智。
我们目前有以下代码
public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(IUserStore<ApplicationUser> store, IOptions<IdentityOptions> optionsAccessor, IPasswordHasher<ApplicationUser> passwordHasher, IEnumerable<IUserValidator<ApplicationUser>> userValidators, IEnumerable<IPasswordValidator<ApplicationUser>> passwordValidators, ILookupNormalizer keyNormalizer, IdentityErrorDescriber errors, IServiceProvider services, ILogger<UserManager<ApplicationUser>> logger) : base(store, optionsAccessor, passwordHasher, userValidators, passwordValidators, keyNormalizer, errors, services, logger)
{
}
public override async Task<IdentityResult> ResetPasswordAsync(ApplicationUser user, string token, string newPassword)
{
var result = await base.ResetPasswordAsync(user, token, newPassword);
if (result.Succeeded)
{
user.ChangePasswordDate = DateTime.Now.AddYears(1);
user.ChangePassword = false;
await base.UpdateAsync(user);
}
return result;
}
}
我如何将其转换为 f#?
如有任何帮助,我们将不胜感激
你好,
格伦
如果您使用的是 F# 6,则可以使用新的 task builder,如下所示:
open Microsoft.AspNetCore.Identity
type ApplicationUserManager(store, optionsAccessor, passwordHasher, userValidators, passwordValidators, keyNormalizer, errors, services, logger) =
inherit UserManager<ApplicationUser>(store, optionsAccessor, passwordHasher, userValidators, passwordValidators, keyNormalizer, errors, services, logger)
member private _.BaseResetPasswordAsync(user, token, newPassword) =
base.ResetPasswordAsync(user, token, newPassword)
override this.ResetPasswordAsync(user, token, newPassword) =
task {
let! result = this.BaseResetPasswordAsync(user, token, newPassword)
if result.Succeeded then
user.ChangePasswordDate <- DateTime.Now.AddYears(1)
user.ChangePassword <- false
let! _result = this.UpdateAsync(user)
ignore _result
return result;
}
需要私有 Base
方法,因为
请注意,您在 C# 代码中忽略了 UpdateAsync
的结果。我在这里做过同样的事情,但我不知道这样做是否明智。