User.Identity.GetUserId()&& User.IsInRole(" rolename")不工作

时间:2017-01-20 13:15:05

标签: c# model-view-controller asp.net-identity identity claims-based-identity

我尝试自定义ASP Identity 2.0以使用Guid Key。我能够登录,但在尝试获取用户数据时遇到了一些问题。

这些都不起作用:

User.Identity.GetUserId() [总是以空字符串形式返回]

User.IsInRole("rolename") [总是返回为假]

填充User.Identity.Name。

我的登录处理程序

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();
    }
}

我的身份代码

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>
    {
    }
}

2 个答案:

答案 0 :(得分:1)

您需要在AccountController中使用此代码

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

答案 1 :(得分:0)

FormsAuthentication是你的问题。可能您的web.config包含与MembershipRoleProvider相关的条目 - 与User.IsInRole混淆。删除与FormsAuthentication相关的所有代码/配置以解决您的问题。

另见answer