首页 > 解决方案 > 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);
    }
}

标签: asp.net-mvcclaims-based-identityclaims

解决方案


推荐阅读