Why does IsInRole always return false?

后端 未结 1 658
执念已碎
执念已碎 2020-12-11 12:45

I am building an MVC5 application with ASP.NET Identity 2.0 and EF 6.1.1.

This is part of my current Login method:

    [HttpPost]
    [AllowAnonymous         


        
1条回答
  •  囚心锁ツ
    2020-12-11 13:04

    I had same kind of issue, then I got work it as below. The reason seems to be, the user not completely signin or completed all authentication process in this stage.

    case SignInStatus.Success:
                    {
                        var user = await UserManager.FindAsync(model.UserName, model.Password);
                        var roles = await UserManager.GetRolesAsync(user.Id)
    
                        if (user == null)
                            break;
    
                        if (roles.Contains("OfficeUser"))
                            break; // << I always hit this line - even if the user is an OfficeUser
    

    0 讨论(0)
提交回复
热议问题