问题
I have just started to know the MVC 5 and I am trying to use its built in owin authentication.
I need to implement a forms authentication with IIS, but the OWIN Authentication is complicated than i waited.
I have az Entity Framework Model with own User, Role and RoleUser tables and want to authenticate user by these tables.
I tried to figured it out, how the owin works on a sample mvc 5 application. It has an ApplicationUser class:
public class ApplicationUser : IdentityUser
{
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
}
My main problem is the IdentityUser. It is an own ASP.NET User class with implementation of IUser interface and connection of DbContext. I have an own User POCO entity from the EF model and i do not want to mix it with an ASP.NET IUser interface. I do know why, but the Id of IUser interface is string type, that is also not apply to me.
The owin async user sign in is the following:
private async Task SignInUserAsync(User user, bool isPersistent)
{
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
ClaimsIdentity identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
}
It recommends to create a ClaimsIdentity type of identity to sign in user and the user property must implements IUser interface.
Big question: Is the owen suggested to use in my environment? I want to use my User Class with owin authentication, but i don't know how?
Thanks your help in advance!
回答1:
You can ditch the entire ASP.NET Identity model and use your own components for password verification and roles/claim storage.
http://www.khalidabuhakmeh.com/asp-net-mvc-5-authentication-breakdown-part-deux
Membership/Identity and Forms Auth are two different features of id and access management. Microsoft documentation does not do a good job explaining the difference between the two. This is a short and sweet post that explains it. The article was written before OWIN, but the same principle applies.
http://brockallen.com/2012/06/04/membership-is-not-the-same-as-forms-authentication/
回答2:
The best way I can figure is to have your POCO class derive from IdentityUser. Doing so you can add your POCO properties as part of your new class. After this, you can instantiate a new UserManager such as:
UserManager um = new UserManager<YourPocoDerivedFromIdentityUser>(new UserStore<YourPocoDerivedFromIdentityUSer>(new ApplicationDbContext())
using OWIN will allow you to easily integrate 3rd-party authentication, such as FB, Google. The OWIN team did a really good job integrating it with MVC.
If, however, your current authentication system works well I don't see why you'd wish to upgrade?
P.S. - http://www.apress.com/files/extra/ASP_NET_Identity_Chapters.pdf
You can find 3 chapters on the new Identity System here. They are a very good read and I highly recommend them for getting you started.
来源:https://stackoverflow.com/questions/20589970/asp-net-mvc-5-owin-auhentication