Skip to content

BiBongNet/ASP.NET-Identity

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

New ASP.NET Identity Custom Implementation

This is an example of custom implementation of new ASP.NET Identity framework which included in ASP.NET with Visual Studio 2013 RC.

Requirements

  • .NET Framework 4.5.1
  • Visual Studio 2013 RC

Project structure

The solution consists of three projects.

Asp.Identity

Initially is the Asp.Identity project that includes all the implementation of core interfaces and has only a dependency on Asp.Identity.Core. There is no dependency on Entity Framework or any other library so it could be used as based project for other solutions, for example in combination with NHibernate. Furthermore the Entities don't have any data access specific code or attributes.

The most important note here is the UserBase class that adds some extra properties just to show how we can extend the basic interfaces.

Asp.Identity.EntityFramework

Then is the Asp.Identity.EntityFramework project that includes a custom implementation of IIdentityStore interface through Entity Framework. Moreover it includes the entities configuration files so to store these in different database tables than the default.

The class IdentityContext is the class that used for all the database interaction and includes code validation for checking duplicate Email which is a custom property.

protected override DbEntityValidationResult ValidateEntity(DbEntityEntry entityEntry, IDictionary items)
{
    if(entityEntry != null && entityEntry.State == EntityState.Added)
    {
        // Validation for duplicate email
        UserBase user = entityEntry.Entity as UserBase;
        if(user != null)
        {
            if(Users.Any(u => u.Email.ToUpper() == user.Email.ToUpper()))
            {
                return new DbEntityValidationResult(entityEntry, new List())
                {
                    ValidationErrors =
                    {
                        new DbValidationError("User", string.Format(CultureInfo.CurrentCulture, "Duplicate Email. Email: {0}", user.Email))
                    }
                };
            }
        }
    }
    return base.ValidateEntity(entityEntry, items);
}

Asp.Identity.EntityFramework.Web

At last is the web project that uses the previous ones during User authentication and authorization.

It is the basic ASP.NET MVC project VS2013 RC offers and the only change is in the Account Controller class.

More specifically in the constructor of the controller we initialize the AuthenticationIdentityManager with the custom implementation of IIdentityStore

public AccountController() 
{
    var identityDbContext = new IdentityContext("DefaultConnection");
    IdentityManager = new AuthenticationIdentityManager(new IdentityStore(identityDbContext));
}

Additionaly there are some changes in RegisterViewModel class that includes the Email field which is required. It is also available the log in functionality through Google.

About

Custom implementation of new ASP.NET Identity Framework

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published