The use of the ASP. NET identity custom user class, Claimsidentity

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.