asp.net-mvc - MVC:编辑用户后无法刷新身份声明(我也是)
问题描述
我有一个身份基础设施的定制实现,其中我的用户有一些属性可以作为声明访问。
其中一些声明可由用户编辑。我的问题是,当用户将更改保存到其个人资料时,声明不会更新。
我在这方面找到了很多答案,几乎所有人都建议注销/登录(或者如果使用 mvc 核心,则使用 RefreshSignInAsync)。
我尝试使用这种方法,但它不起作用 - 请参阅我的控制器中的 EditProfile 方法。
我认为它与 User 属性有关,但我不知道如何刷新它......
控制器:
[SGVAuthorize]
[OutputCache(VaryByParam = "*", Duration = 0, NoStore = true)]
public class AccountController : Controller
{
private SGVSignInManager _signInManager;
private SGVUserManager _userManager;
public SGVSignInManager SignInManager
{
get
{
return _signInManager ?? (_signInManager = HttpContext.GetOwinContext().Get<SGVSignInManager>());
}
private set
{
_signInManager = value;
}
}
public SGVUserManager UserManager
{
get
{
return _userManager ?? (_userManager = HttpContext.GetOwinContext().GetUserManager<SGVUserManager>());
}
private set
{
_userManager = value;
}
}
private IAuthenticationManager AuthenticationManager
{
get
{
return HttpContext.GetOwinContext().Authentication;
}
}
/*...*/
public async Task<ActionResult> EditProfile(EditProfileViewModel model, string returnUrl)
{
if (ModelState.IsValid)
{
var user = await UserManager.FindByIdAsync(User.Identity.GetSGVUserId());
user.MyClaimVal = model.MyClaimVal;
var result = await UserManager.UpdateAsync(user);
if (result.Succeeded)
{
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
var identity = (ClaimsIdentity)User.Identity;
var v = identity.Claims.First(x=>x.Type == "MyClaim").Value; /* STILL OLD VALUE !!!!!!!!!!!!!! */
return RedirectToLocal(returnUrl);
}
AddErrors(result);
}
return View(model);
}
}
用户:
public class SGVUser : IUser<SGVUserId>
{
public string MyClaimVal {get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<SGVUser, SGVUserId> manager)
{
/* ... */
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
userIdentity.AddClaim(new Claim("MyClaim", MyClaimVal);
/* ... */
return userIdentity;
}
public static string GetProp(this IIdentity identity, string Property)
{
try
{
var claim = ((ClaimsIdentity)identity).FindFirst(Property);
return (claim != null) ? claim.Value : string.Empty;
}
catch
{
return string.Empty;
}
}
public static string GetMyClaim(this IIdentity identity)
{
string v = GetProp(identity, "MyClaim");
return v;
}
}
用户管理器:
public class SGVUserManager : UserManager<SGVUser, SGVUserId>
{
public override async Task<IdentityResult> UpdateAsync(SGVUser user)
{
/* ... */
return await base.UpdateAsync(user);
/* ... */
}
}
登录管理器:
public class SGVSignInManager : SignInManager<SGVUser, SGVUserId>
{
public override Task<ClaimsIdentity> CreateUserIdentityAsync(SGVUser user)
{
return user.GenerateUserIdentityAsync((SGVUserManager)UserManager);
}
}
店铺:
public class SGVUserStore :
IUserStore<SGVUser, SGVUserId>,
IUserPasswordStore<SGVUser, SGVUserId>,
IUserLockoutStore<SGVUser, SGVUserId>,
IUserTwoFactorStore<SGVUser, SGVUserId>,
IUserEmailStore<SGVUser, SGVUserId>
{
public Task UpdateAsync(SGVUser user)
{
UpdateInDb(user.MyClaimVal);
return Task.FromResult(0);
}
}
解决方案
推荐阅读
- spring-boot - SpringBoot 不接自定义转换器
- javascript - 如何为 Lodash 流中的每个函数做一些事情?
- c# - RazorEngine.RunCompile() 如何在幕后工作?
- java - 插入行后获取 id(Spring boot/JPA)
- nginx - 如何配置 Nginx 以进行页面刷新/重新加载
- html - 如何翻译 HTML 文件?
- android - 从 2.3.3 升级到 Gradle 插件到 3.4.2 时出现重复的类
- ios - 如何以编程方式为 JSONmodel 创建文件并自动创建其 Codable 结构
- r - 如何通过 purrr 替换修改 for 循环
- c++ - 编译器错误:'std::array<...>::~array()' 被隐式删除