如何在 .NET Core Razor Pages 中使用 Setters 扩展 UserManager?
How to extend UserManager with Setters in .NET Core Razor Pages?
我正在使用 .NET Core 2 Razor Pages 和个人帐户身份验证开发 Web 应用程序。
我用名字扩展了我的数据库,因为它不是按标准实现的。一切正常。但是,我想扩展我的 /Account/Manage 页面以确保用户能够更改他们自己的名字。
OnGetAsync 工作正常,但当我的 OnPostAsync 无法正常工作时。
OnPostAsync
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
var user = await _userManager.GetUserAsync(User);
if (user == null)
{
throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
}
if (Input.Email != user.Email)
{
var setEmailResult = await _userManager.SetEmailAsync(user, Input.Email);
if (!setEmailResult.Succeeded)
{
throw new ApplicationException($"Unexpected error occurred setting email for user with ID '{user.Id}'.");
}
}
if (Input.PhoneNumber != user.PhoneNumber)
{
var setPhoneResult = await _userManager.SetPhoneNumberAsync(user, Input.PhoneNumber);
if (!setPhoneResult.Succeeded)
{
throw new ApplicationException($"Unexpected error occurred setting phone number for user with ID '{user.Id}'.");
}
}
// not working yet
if (Input.FirstName != user.FirstName)
{
var setFirstNameResult = await MyManager.SetFirstNameAsync(user, Input.FirstName);
if (!setFirstNameResult.Succeeded)
{
throw new ApplicationException($"Unexpected error occurred setting first name for user with ID '{user.Id}'.");
}
}
StatusMessage = "Your profile has been updated";
return RedirectToPage();
}
MyManager.cs
public class MyManager : UserManager<ApplicationUser>
{
public MyManager(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 static async Task<IdentityResult> SetFirstNameAsync(ApplicationUser user, string FirstName)
{
if (user == null)
{
throw new ArgumentNullException(nameof(user));
}
if (FirstName == null)
{
throw new ArgumentNullException(nameof(FirstName));
}
user.FirstName = FirstName;
return IdentityResult.Success;
}
}
当我点击 'Save' 按钮并告诉我我的个人资料已成功更新时,它失败了,但它没有。它只是保留了旧的名字值。我在这里错过了什么?
您的方法与类似的内置方法不同:即实际上将更改持久保存回数据库。您只需在用户上设置 属性 然后返回成功。一旦该实例超出范围,您为名字设置的值就会随之消失。
也就是说,@CodeNotFound 的评论很重要。没有理由这样做,你也不应该这样做。其他方法适用于特定用例。对于一般更新用户的属性,您应该简单地设置相关的 属性,然后使用 UpdateAsync
将用户保存回来。
我正在使用 .NET Core 2 Razor Pages 和个人帐户身份验证开发 Web 应用程序。
我用名字扩展了我的数据库,因为它不是按标准实现的。一切正常。但是,我想扩展我的 /Account/Manage 页面以确保用户能够更改他们自己的名字。
OnGetAsync 工作正常,但当我的 OnPostAsync 无法正常工作时。
OnPostAsync
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
var user = await _userManager.GetUserAsync(User);
if (user == null)
{
throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
}
if (Input.Email != user.Email)
{
var setEmailResult = await _userManager.SetEmailAsync(user, Input.Email);
if (!setEmailResult.Succeeded)
{
throw new ApplicationException($"Unexpected error occurred setting email for user with ID '{user.Id}'.");
}
}
if (Input.PhoneNumber != user.PhoneNumber)
{
var setPhoneResult = await _userManager.SetPhoneNumberAsync(user, Input.PhoneNumber);
if (!setPhoneResult.Succeeded)
{
throw new ApplicationException($"Unexpected error occurred setting phone number for user with ID '{user.Id}'.");
}
}
// not working yet
if (Input.FirstName != user.FirstName)
{
var setFirstNameResult = await MyManager.SetFirstNameAsync(user, Input.FirstName);
if (!setFirstNameResult.Succeeded)
{
throw new ApplicationException($"Unexpected error occurred setting first name for user with ID '{user.Id}'.");
}
}
StatusMessage = "Your profile has been updated";
return RedirectToPage();
}
MyManager.cs
public class MyManager : UserManager<ApplicationUser>
{
public MyManager(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 static async Task<IdentityResult> SetFirstNameAsync(ApplicationUser user, string FirstName)
{
if (user == null)
{
throw new ArgumentNullException(nameof(user));
}
if (FirstName == null)
{
throw new ArgumentNullException(nameof(FirstName));
}
user.FirstName = FirstName;
return IdentityResult.Success;
}
}
当我点击 'Save' 按钮并告诉我我的个人资料已成功更新时,它失败了,但它没有。它只是保留了旧的名字值。我在这里错过了什么?
您的方法与类似的内置方法不同:即实际上将更改持久保存回数据库。您只需在用户上设置 属性 然后返回成功。一旦该实例超出范围,您为名字设置的值就会随之消失。
也就是说,@CodeNotFound 的评论很重要。没有理由这样做,你也不应该这样做。其他方法适用于特定用例。对于一般更新用户的属性,您应该简单地设置相关的 属性,然后使用 UpdateAsync
将用户保存回来。