为什么IsInRole总是返回false? [英] Why does IsInRole always return false?

查看:203
本文介绍了为什么IsInRole总是返回false?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ASP.NET Identity 2.0和EF 6.1.1构建一个MVC5应用程序。



这是我当前登录方法的一部分:

  [HttpPost] 
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async任务< ActionResult>登录(LoginViewModel模型,字符串returnUrl)
{
if(!ModelState.IsValid)
return View(model);

var result = await SignInManager.PasswordSignInAsync(model.UserName,model.Password,model.RememberMe,shouldLockout:true);
switch(result)
{
case SignInStatus.Success:
{
var user = await UserManager.FindAsync(model.UserName,model.Password);

if(user == null)
break;

if(!UserManager.IsInRole(user.Id,OfficeUser))
break; //<<我总是打这个行 - 即使用户是OfficeUser

这样工作正常,直到我打到UserManager。 IsInRole()。这总是返回false。



在某个地方我读到IsInRole()将失败,如果相应的用户没有登录,但由于我通过SignInManager.PasswordSignInAsync()我相信这应该是罚款。 / p>

是的,我已经检查了数据库多次,非常仔细;)我的测试用户和我的测试角色OfficeUser绝对分配给彼此。

有人有想法吗?

解决方案

我有同样的问题我得到如下工作。原因似乎是,用户在这个阶段没有完全登录或完成所有的认证过程。

  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; //<<我总是打这个行 - 即使用户是OfficeUser


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]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    {
        if (!ModelState.IsValid)
            return View(model);

        var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, shouldLockout: true);
        switch (result)
        {
            case SignInStatus.Success:
                {
                    var user = await UserManager.FindAsync(model.UserName, model.Password);

                    if (user == null)
                        break;

                    if (!UserManager.IsInRole(user.Id, "OfficeUser"))
                        break; // << I always hit this line - even if the user is an OfficeUser

This works fine until I hit UserManager.IsInRole(). This always returns false.

Somewhere I read that IsInRole() will fail if the corresponding user is not signed in. But since I pass SignInManager.PasswordSignInAsync() I believe this should be fine.

And yes, I have checked the database many times and very carefully ;) My test user and my test role "OfficeUser" are definitely assigned to each other.

Does anybody have an idea?

解决方案

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

这篇关于为什么IsInRole总是返回false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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