简体   繁体   中英

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

I'm trying to customize ASP Identity 2.0 to work with a 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.IsInRole("rolename") [always returns as false]

User.Identity.Name is populated.

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

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

FormsAuthentication is your problem. Probably your web.config contains entries related to MembershipRoleProvider - that messes with User.IsInRole . Remove all code/config related to FormsAuthentication to fix your problem.

Also see this answer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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