GeekInTheBox
GeekInTheBox

Reputation: 149

Add profile fields in ASP.NET Identity in WebForms C#

Can anyone provide a good resource for setting up profile fields (ie FirstName, LastName, et cetera) in ASP.NET Identity using C# in Web Forms? Everything I've been able to find is either MVC specific or doesn't address profiles.

Upvotes: 0

Views: 549

Answers (1)

Anton Gorbunov
Anton Gorbunov

Reputation: 1444

You may use Identity Claims for storing profile information. This is not a very nice solution, because it generates a lot of joins when user gets from DB. But up to certain sizes of the users database, you will not see a difference in performance.

Some code for example:

public class ApplicationUser : IdentityUser
{
    ppublic async Task<ClaimsIdentity> AssignUserIdentityAsync(UserManager<ApplicationUser> manager)
    {        
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);     
        userIdentity.AddClaim(new Claim("FirstName", this.FirstName.ToString()));

        return userIdentity;
    }

    // Custom filed
    public long? FirstName { get; set; }
    //and so on
}

namespace Extensions
{
    public static class IdentityExtensions
    {
        public static string GetFirstName(this IIdentity identity)
        {
            var claim = ((ClaimsIdentity)identity).FindFirst("FirstName");            
            return claim ?? string.Empty;
        }

        // and so on
    }
}

Upvotes: 1

Related Questions