ASP. NET Identity Role-rights Management 4

Source: Internet
Author: User

1.1. User-role Analysis

Presumably everyone has noticed that Microsoft.AspNet.Identity.EntityFramework is the EF implementation of Microsoft.AspNet.Identity.Core, how Microsoft handles Identityuser and Identityrole The relationship? Because the two are many-to-many relationships, an association table is added to the relational database, and the Identityuserrole is added, and the Identityuserrole list in Identityuser and Identityrole is shown below.

public class Identityuserrole<tkey>

{

Public virtual TKey Roleid {get; set;}

Public virtual TKey UserId {get; set;}

}

Identityuser

public class Identityuser<tkey, Tlogin, Trole, tclaim>: iuser<tkey> where Tlogin:identityuserlogin<tkey > Where trole:identityuserrole<tkey> where tclaim:identityuserclaim<tkey>

{

Public Identityuser ()

{

This. Roles = new list<trole> ();

}

Public icollection<trole> Roles {virtual get; private set;}

Other code omitted ....

}

Identityrole

public Class Identityrole<tkey, tuserrole>: irole<tkey> where tuserrole:identityuserrole<tkey>

{

    public identityrole ()

    {

        this. Users = new list<tuserrole> ();

   }

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;

    public TKey Id {get; set;}

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;

    public string Name {get; set;}

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;

    public icollection<tuserrole> Users {virtual get; private set;}

}

EF Configures 1-to-many relationships for Identityuser, Identityrole, and Identityuserrole, respectively.

public class Identitydbcontext:dbcontext

{

Public Identitydbcontext (): This ("DefaultConnection")

{

}

protected override void Onmodelcreating (Dbmodelbuilder modelBuilder)

{

if (ModelBuilder = = null)

{

throw new ArgumentNullException ("ModelBuilder");

}

entitytypeconfiguration<tuser> configuration = modelbuilder.entity<tuser> (). ToTable ("Aspnetusers");

Configuration. hasmany<tuserrole> (U = u.roles). Withrequired (). hasforeignkey<tkey> (ur = ur). USERID);

Indexattribute Indexattribute = new Indexattribute ("Usernameindex") {

IsUnique = True

};

Configuration. Property ((Expression<func<tuser, string>>) (U = u.username)). IsRequired (). Hasmaxlength (0x100). Hascolumnannotation ("Index", New Indexannotation (Indexattribute));

Configuration. Property ((Expression<func<tuser, string>>) (U = u.email)). Hasmaxlength (0x100);

Modelbuilder.entity<tuserrole> (). Haskey (r = new {UserId = R.userid, Roleid = R.roleid}). ToTable ("Aspnetuserroles");

entitytypeconfiguration<trole> Configuration2 = modelbuilder.entity<trole> (). ToTable ("Aspnetroles");

Indexattribute attribute2 = new Indexattribute ("Rolenameindex") {

IsUnique = True

};

Configuration2. Property ((Expression<func<trole, string>>) (r = r.name)). IsRequired (). Hasmaxlength (0x100). Hascolumnannotation ("Index", New Indexannotation (Attribute2));

Configuration2. Hasmany<tuserrole> (r = r.users). Withrequired (). hasforeignkey<tkey> (ur = ur). Roleid);

}

Public virtual idbset<trole> Roles {get; set;}

Public virtual idbset<tuser> Users {get; set;}

}

Imitate the above design, realize role-permission relation.

ASP. NET Identity Role-rights Management 4

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.