简体   繁体   English

User.Identity.GetUserId()&& User.IsInRole(“角色名称”)不起作用

[英]User.Identity.GetUserId() && User.IsInRole(“rolename”) Not Working

I'm trying to customize ASP Identity 2.0 to work with a Guid Key. 我正在尝试自定义ASP Identity 2.0以与Guid Key一起使用。 I'm able to log in a-ok, but have a couple of problems when I try to obtain User data. 我可以正常登录,但是在尝试获取用户数据时遇到了一些问题。

Neither of these work: 这些都不起作用:

User.Identity.GetUserId() [always returns as an empty string] User.Identity.GetUserId() [始终以空字符串形式返回]

User.IsInRole("rolename") [always returns as false] User.IsInRole("rolename") [总是返回false]

User.Identity.Name is populated. 填充User.Identity.Name。

My login handler 我的登录处理程序

public class CommandHandler : IRequestHandler<ViewModel, Result>
{
    private readonly GuidUserManager _manager;

    public CommandHandler(GuidUserManager manager)
    {
        _manager = manager;
    }

    public Result Handle(ViewModel criteria)
    {
        // Verify if a user exists with the provided identity information
        var user = _manager.Find(criteria.UserName, criteria.Password);

        // If a user was found
        if (user == null)
            return Result.Fail("Invalid username or password.");

        // Clear any lingering authencation data
        FormsAuthentication.SignOut();

        // Write the authentication cookie
        FormsAuthentication.SetAuthCookie(user.UserName, criteria.RememberMe);

        return Result.Ok();
    }
}

My Identity code 我的身份验证码

using System;
using System.Data.Entity;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;

namespace FM.Web.Security
{
    public class GuidUserDbContext : IdentityDbContext<GuidUser, GuidRole, Guid, GuidUserLogin, GuidUserRole, GuidUserClaim>
    {
        public GuidUserDbContext(string connection)
            : base(connection)
        {
            Database.SetInitializer(new GuidUserDbContextInitialilser());
        }
    }

    public class GuidUserDbContextInitialilser : CreateDatabaseIfNotExists<GuidUserDbContext>
    {
    }

    public class GuidUserManager : UserManager<GuidUser, Guid>
    {
        public GuidUserManager(GuidUserStore userStore)
            : base(userStore)
        {

        }
    }

    public class GuidUserLogin : IdentityUserLogin<Guid>
    {
    }

    public class GuidUserRole : IdentityUserRole<Guid>
    {
    }

    public class GuidUserClaim : IdentityUserClaim<Guid>
    {
    }

    public class GuidRole : IdentityRole<Guid, GuidUserRole>
    {
    }

    public class GuidUserStore : UserStore<GuidUser, GuidRole, Guid, GuidUserLogin, GuidUserRole, GuidUserClaim>
    {
        public GuidUserStore(DbContext context)
            : base(context)
        {
        }
    }

    public class GuidRoleStore : RoleStore<GuidRole, Guid, GuidUserRole>
    {
        public GuidRoleStore(DbContext context)
            : base(context)
        {
        }
    }

    public class GuidUser : IdentityUser<Guid, GuidUserLogin, GuidUserRole, GuidUserClaim>
    {
    }
}

You need this code in AccountController 您需要在AccountController中使用此代码

public GuidUserManager UserManager
{
    get
    {
        return _userManager ?? HttpContext.GetOwinContext().GetUserManager<GuidUserManager>();
    }
    private set
    {
        _userManager = value;
    }
}

FormsAuthentication is your problem. FormsAuthentication是您的问题。 Probably your web.config contains entries related to MembershipRoleProvider - that messes with User.IsInRole . 您的web.config可能包含与MembershipRoleProvider相关的条目-与User.IsInRole Remove all code/config related to FormsAuthentication to fix your problem. 删除所有与FormsAuthentication相关的代码/配置,以解决您的问题。

Also see this answer 另请参阅此答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM