asp.net-mvc-5 – MVC 5 AddToRole在工作之前需要注销?

我发现如果我添加一个用户到一个角色在ASP身份,它不会生效,直到我退出并重新登录.我需要做的是刷新用户的角色,而不强制用户登录首先关闭

以下是我将用户添加到角色的方法.

var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
var userId = HttpContext.Current.User.Identity.GetUserId();

userManager.AddToRole(userId, roleName);

然后,几乎立即,我将用户重定向到此操作方法.我可以在数据库中告诉我已经被添加到正确的角色,但MVC仍然将我重定向到登录页面.但是,如果我注销,请重新登录,并尝试去执行此操作方法,它的工作正常.

    [Authorize(Roles = RecoveryStandardRoles.ServiceProvider)]

    public partial class CertifyController : Controller

{
    #region Public Methods and Operators

    public virtual ActionResult CompanyProfile()
    {
        return this.View();
    }

    #endregion
}

谢谢你花时间看我的问题!

MVC5注册新用户,分配角色并激活具有角色的用户,不使用以下命令退出登录:await UserManager.AddToRoleAsync(user.Id,“角色名称”)

if (ModelState.IsValid)
{
    var user = new ApplicationUser() { UserName = model.Email, Email = model.Email,StandName = model.StandName,FirstName = model.FirstName,LastName = model.LastName,CellPhone = model.CellPhone,Supervisor = model.Supervisor};
    IdentityResult result = await UserManager.CreateAsync(user, model.Password);

    var roleStore = new RoleStore<IdentityRole>(context);
    var roleManager = new RoleManager<IdentityRole>(roleStore);

    var userStore = new UserStore<ApplicationUser>(context);
    var userManager = new UserManager<ApplicationUser>(userStore);


    if (result.Succeeded)
    {
         ***await UserManager.AddToRoleAsync(user.Id, "Users Tammy");***
         await SignInAsync(user, isPersistent: false);
翻译自:https://stackoverflow.com/questions/20495249/mvc-5-addtorole-requires-logout-before-it-works

转载注明原文:asp.net-mvc-5 – MVC 5 AddToRole在工作之前需要注销?