0

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>
    {
    }
}
ETFairfax
  • 3,794
  • 8
  • 40
  • 58

2 Answers2

1

You need this code in AccountController

public GuidUserManager UserManager
{
    get
    {
        return _userManager ?? HttpContext.GetOwinContext().GetUserManager<GuidUserManager>();
    }
    private set
    {
        _userManager = value;
    }
}
Dawid Rutkowski
  • 2,658
  • 1
  • 29
  • 36
kgzdev
  • 2,770
  • 2
  • 18
  • 35
  • 1
    Actually it's not required. Maybe he is registering GuidUserManager in his IoC container and in that case it will be injected into constructor. – Dawid Rutkowski Jan 20 '17 at 13:56
0

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

Community
  • 1
  • 1
trailmax
  • 34,305
  • 22
  • 140
  • 234