I build MyApplication in layered style keeping the responsibility separate from other layers, I've added separate ClassLibraries for my DAL and ModelLayer.
In my DataAccessLayer have Entity-Framework package installed, migration enabled and ModelLayer is referened in the DAL. I'm successfully able to do Automatic/Manual Migrations and maintain DB structure.
Now I want to add Identity Framework to incorporate Authentication & Authorization into my project. My question is, which layer should I add it on?
Following are my changes to add Identity, Please have a look. I appreciate Any suggestions or correction in my approach.
I'm new to .Net framework so please guide me if m wrong with my approach. I've tried implementing Identity Framework using ASP.MVC 5, Code-First Approach. I believe I'm half way through but while testing it, Every time I try to register user it fails in Account/Register()
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Email,
Email = model.Email };
var result = await UserManager.CreateAsync(user,
model.Password);
Here------> if (result.Succeeded) <---- Here To be precise
{
await SignInManager.SignInAsync(user,
isPersistent:false,
rememberBrowser:false);
}
}}
I've gone through different articles and learned the basics of Identity framework and how to do it. Installed following packages into DAL layer
PM> Install-Package Microsoft.AspNet.Identity.EntityFramework
PM> Install-Package Microsoft.AspNet.Identity.Owin
PM> Install-Package Microsoft.Owin.Host.SystemWeb
Moved WebApp/Models/IdentityModel.cs
classes, which comes with the ASP.net MVC 5 template, moved it into my Model Library and AppDbContext class into my DAL library.
IdentityModel.cs Looks like
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("MyConnectionName", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
Made by Context class extend ApplicationDbContext class defined in IdentityModel above.
public class Context : ApplicationDbContext
{
public Context() : base()
{
}
}
Also added IdentityConfig class in App_Start
folder, that is to be used by OWIN
public class IdentityConfig
{
public void Configuration(IAppBuilder app)
{
app.CreatePerOwinContext(() => new ApplicationDbContext());
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<RoleManager<AppRole>>((options, context) =>
new RoleManager<AppRole>(
new RoleStore<AppRole>(context.Get<ApplicationDbContext>())));
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Home/Login"),
});
}
}
and referenced it in web.config
to be visible to OWIN
<appSettings>
<!-- other settings -->
<add key="owin:AppStartup" value="MyAppNamespace.IdentityConfig" />
</appSettings>
I've also updated my Database which added Identity Tables into my Database
i.e AspNetRoles
AspNetUserClaims
AspNetLogins
AspNetUserRoles
AspNetUsers