Neo
Neo

Reputation: 11

How can I use my database tables etc Users table with ASP.NET Identity?

I have following tables:

Users, Groups, GroupUsers

I have my class like:

public class User
{
   List<int> GroupIds;
}

I have a method in my Data Access Layer which returns a user after successful login with the list of all the group ids. How can I override or do something similar in identity to get information from that method?

I have searched the Internet a lot, watched YouTube tutorials, but no one is actually explaining it the way I need it. Can any one help?

Upvotes: 0

Views: 423

Answers (3)

Ha Doan
Ha Doan

Reputation: 671

Create a UserInfo object

public class ApplicationUser : IdentityUser
{
    public virtual UserInfo UserInfo { get; set; }
}

public class UserInfo : ComparableEntity
{
    public string Email { get; set; }
    public string FullName { get; set; }
    public string KidName { get; set; }
    public string MobilePhone { get; set; }
}

Then create a database context

public class DatabaseContext : IdentityDbContext<ApplicationUser>, IDatabaseContext
{
    public IDbSet<UserInfo> UserInfos { get; set; }
}

Upvotes: 0

BlackICE
BlackICE

Reputation: 8916

First, use claims based security instead of role based: http://visualstudiomagazine.com/articles/2013/08/01/leveraging-claims-based-security-in-aspnet-45.aspx

I've also been retro-fitting an existing SQL based login system to work with identity management. Most of the work you're going to have to do lies within IdentityModel.cs. Specifically how you define ApplicationUser

public class ApplicationUser : IdentityUser<string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(ApplicationUserManager manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }
}

I've overridden all 4 type parameters on the IdentityUser base class for my implementation, you may not need to do so, depends on how different your retrofit is from how Identity expects things to be.

The other main place you'll most likely have to customize things is within ApplicationDbContext where you'll have to set up where your user, group, and claims/role definitions reside with SQL.

I found these articles on typecastexception to be very useful:

http://typecastexception.com/post/2014/04/20/ASPNET-MVC-and-Identity-20-Understanding-the-Basics.aspx

http://typecastexception.com/post/2014/06/22/ASPNET-Identity-20-Customizing-Users-and-Roles.aspx

http://typecastexception.com/post/2014/04/20/ASPNET-Identity-20-Setting-Up-Account-Validation-and-Two-Factor-Authorization.aspx

http://typecastexception.com/post/2014/07/13/ASPNET-Identity-20-Extending-Identity-Models-and-Using-Integer-Keys-Instead-of-Strings.aspx

Overall there going to be a lot of trial and error in the process as you figure out what pieces of Identity you can utilize as is and where you need to plug in your own code.

Something else you may run into if your passwords aren't stored is having to provide your own implementation of PasswordHasher and plugging that in:

Asp.net Identity password hashing

Upvotes: 1

Damiano C.
Damiano C.

Reputation: 282

I did' t get your question, if you want to override you have to mark the method virtual and inherit the class like this:

public class User
{
  public virtual void YourMethod()
  {
  }
}

public class YourClass : User
{
  public override void YourMethod()
  {
  }
}

If you want to separate the class to add some more mothods you can go like this:

partial class User
{
    public static void YourMethod()
    {

    }
}

Upvotes: 0

Related Questions