MVC5 automatically generated user authentication is more useful and can be extended, but requires code first, currently using SQLite, support Entity Framework, but does not support code first.
It is only a simple imitation of yourself. After the experiment, the following several, can run.
Webconfig
<appSettings> <AddKey= "Webpages:version"value= "3.0.0.0" /> <AddKey= "webpages:enabled"value= "false" /> <AddKey= "clientvalidationenabled"value= "true" /> <AddKey= "unobtrusivejavascriptenabled"value= "true" /> <AddKey= "Owin:automaticappstartup"value= "true" /> </appSettings>
Startup.Auth.cs
Public Partial class startup{ publicvoid Configureauth (Iappbuilder app) { app. Usecookieauthentication (new cookieauthenticationoptions { = Defaultauthenticationtypes.applicationcookie, new pathstring ("/account/ Login"), });
}}
Claim identity-related interfaces, method
Public Interfaceiuseridentity {stringId {Get;Set; } stringUserName {Get;Set; } } Public StaticClaimsidentity createidentity<tuseridentity>(tuseridentity user)wheretuseridentity:iuseridentity {claimsidentity identity=Newclaimsidentity (Defaultauthenticationtypes.applicationcookie); Identity. Addclaim (NewClaim ("Http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider","ASP . NET Identity")); Identity. Addclaim (NewClaim (claimtypes.name, user. UserName)); Identity. Addclaim (NewClaim (claimtypes.nameidentifier, user. ID)); //identity. Addclaim (New Claim (Claimtypes.primarysid, user. ID)); //identity. Addclaim (New Claim ("DisplayName", user. UserName)); returnidentity; }
Signinmanager
PrivateIauthenticationmanager AuthenticationManager {Get{returnHttpcontext.getowincontext (). authentication; } } varuser =Appuseridentityservice.findasync (Loginviewmodel.username, Loginviewmodel.password, DB. Myusers); if(User = =NULL|| User. Result = =NULL) Modelstate.addmodelerror ("","The user name or password is incorrect"); Else{claimsidentity Identity=appuseridentityservice.createidentity (user. Result); Authenticationmanager.signout (Defaultauthenticationtypes.applicationcookie); Authenticationmanager.signin (NewAuthenticationproperties () {ispersistent =Loginviewmodel.rememberme}, Identity); returnredirecttolocal (RETURNURL); }
The use of the ASP. NET identity custom user class, Claimsidentity