asp.net identity (Microsoft's most-proven authentication) 2.0 analysis-based on vs2015 default program __.net

Source: Internet
Author: User
Tags class definition

Because it always feels like the asp.net identity is not comfortable to use, such as the extensibility of the Code, and the maintenance of later versions, so analyze it

Enter the following text:

In the default program of VS2015, App_start/identityconfig.cs, through the Applicationusermanager create function, the main content of the function is as follows

  public static Applicationusermanager Create (identityfactoryoptions<applicationusermanager> options, Iowincontext context) 
        {
            var manager = new Applicationusermanager (New userstore<applicationuser> ( Context. Get<applicationdbcontext> ()));
..........

It can be concluded that the function initializes, creates a new Applicationusermanager class (the class itself), passes in a new Userstore generic class, and gives the Userstore generic class A view of the class definition, passed in DbContext Context Parameters

In the Microsoft.AspNet.Identity.EntityFramework binary DLL, the userstore new user's function is

Public async Virtual Task createasync (TUser user)
        {(
            userstore<tuser, Trole, TKey, Tuserlogin, Tuserrole, tuserclaim>) this). Throwifdisposed ();
            if (user = = null)
            {
                throw new ArgumentNullException ("user");
            }
            ((Userstore<tuser, Trole, TKey, Tuserlogin, Tuserrole, tuserclaim>) this). _userstore.create (user);
            Await (Userstore<tuser, Trole, TKey, Tuserlogin, Tuserrole, tuserclaim>) this). SaveChanges (). Withcurrentculture ();
        }

You can see that the key to the operation of the database is _userstore.create, while the _userstore field is initialized to

This._userstore = new entitystore<tuser> (context);
In addition to the management of roles, the _rolestore field
This._rolestore = new entitystore<trole> (context);

Entitystore generics, manipulating the database through an internal call to EntityFramework

        public void Create (tentity entity)
        {this
            . Dbentityset.add (entity);
        }
        

It's a way to know that you're building your own operations on the database and integrating with ASP.net identity




Attach the Entitystore code

Namespace Microsoft.AspNet.Identity.EntityFramework {using System;
    Using System.Data.Entity;
    Using System.Linq;
    Using System.Runtime.CompilerServices;
    
    Using System.Threading.Tasks;
        Internal class entitystore<tentity> where Tentity:class {public entitystore (DbContext context) {this.
            context = context; This. Dbentityset = context.
        Set<tentity> (); } public void Create (tentity entity) {this.
        Dbentityset.add (entity); } public void Delete (tentity entity) {this.
        Dbentityset.remove (entity); Public virtual task<tentity> Getbyidasync (object ID) {return this.
        Dbentityset.findasync (new object[] {ID});
                public virtual void Update (tentity entity) {if (entity!= null) { This. context.entry<tentity> (entity).
            state = entitystate.modified;
        
        } public DbContext context {get; private set;}
        
        Public dbset<tentity> Dbentityset {get; private set;} Public iqueryable<tentity> EntitySet {get {return this.
            Dbentityset; }
        }
    }





Related Article

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.