在ASP.NET框架的身份改变时刷新当前用户的角色? [英] Refresh current user's role when changed in ASP.NET identity framework?

查看:121
本文介绍了在ASP.NET框架的身份改变时刷新当前用户的角色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用VS 2013年,标准的MVC模板,身份提供框架

该用户登录,并且我有:

The user is logged in, and I have:

//....
UserManager.AddToRole(User.Identity.GetUserId(), "Members");       # Line X
RedirectToAction("Index", "Members");

和成员控制器如下:

[Authorize(Roles="Members")]
public class MembersController : Controller
{
    // GET: Members
    public ActionResult Index()
    {
        return View();
    }
}

行X被执行后,我可以证实,该用户将被添加到表 dbo.AspNetUserRoles 。然而,在到达成员控制器用户未能角色检查。 User.IsInRole(成员​​)返回false。

After Line X is executed, I can confirm that the user is added to the table dbo.AspNetUserRoles. However, the user upon reaching the Members controller fails the role check. User.IsInRole("Members") returns false.

如果在用户注销,然后重新登录,然后访问成员控制器将通过,即 User.IsInRole(成员​​)现在返回true。

If the user logs off and then logs in again, then access to the Members controller will go through, ie User.IsInRole("Members") now returns true.

有一些缓存?为什么延误?我该如何克服?

Is there some caching? Why the delay? How do I overcome it?

我也试过转换方法为直线X到异步方法和使用 UserManager.AddToRoleAsync 。同样的延迟效应依然存在。

I also tried converting the method at Line X to an async method and used UserManager.AddToRoleAsync. The same delayed effect is still there.

推荐答案

的身份信息(角色,索赔)放入该cookie时,在用户登录。由于用户已经登录,这条线$ C $的ç UserManager.AddToRole(User.Identity.GetUserId(),成员)将更新数据库,而不是饼干。你必须要重新发行的cookie。

The identity information(roles,claims) are put into the cookie when the user logs in. Since the user is already logged in, this line of code UserManager.AddToRole(User.Identity.GetUserId(), "Members") will update the db , but not the cookie. You have to have to re-issue the cookie.

尝试添加 SignInManager.SignIn(用户,假的,假的); (如果你没有用户, VAR用户= UserManager.FindById(用户.Identity.GetUserId()))之前 RedirectToAction(指数,成员);

Try add SignInManager.SignIn(user, false, false); (if you dont have user, var user = UserManager.FindById(User.Identity.GetUserId())) before RedirectToAction("Index", "Members");

这篇关于在ASP.NET框架的身份改变时刷新当前用户的角色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆