How can I store different pieces of information depending on a user's role in ASP.NET?

时间:2015-05-24 21:28:05

标签: c# asp.net asp.net-mvc asp.net-identity

I am using ASP.NET's built-in identities system. And I want to have different types of users. ASP.NET already allows me to create user roles, but I also want to store different types of data depending on which role the user has.

For example, a customer can have a balance, while a supplier can have a company name. But both are users which should be able to log into my system. Is there a good way to do this in ASP.NET?

1 个答案:

答案 0 :(得分:0)

Use the asp.net identity profile API to add custom properties for your users.

http://www.asp.net/mvc/overview/security/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2-and-openid-sign-on#ap

public class ApplicationUser : IdentityUser
{
    // start custom properties
    public string HomeTown { get; set; }
    public System.DateTime? BirthDate { get; set; }
    // end custom properties
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> 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;
    }
}

This post from the asp.net team has a more step by step approach.

http://blogs.msdn.com/b/webdev/archive/2013/10/16/customizing-profile-information-in-asp-net-identity-in-vs-2013-templates.aspx