Update User Claim not Taking Effect. Why?

后端 未结 3 648
误落风尘
误落风尘 2020-12-08 15:29

I am using ASP.NET MVC 5.1 with Owin and Claims authentication.

After the user changes its email I need to update the users claims, so I tried in the controller:

3条回答
  •  误落风尘
    2020-12-08 16:12

    This works for me. Not sure if it is the best way but the updated claim is in the DB and in subsequent controllers.

    var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
    var c = identity.Claims.FirstOrDefault(r => r.Type == "tId");
    await UserManager.RemoveClaimAsync(user.Id, c);
    await UserManager.AddClaimAsync(user.Id, new Claim("tId", "9032C945-DC5C-4FC9-BE7C-8EDC83A72E58"));
    
    AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
    var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
    AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = isPersistent }, identity);
    

提交回复
热议问题