entity framework 6 - Understanding Asp.Net Identity key points -


i asp.net developer new asp.net identity framework. have been studying sample application , followed tutorials on identity still not able grasp concept completely. have firm grip on asp.net membership identity seems nothing membership. explain have done far.

i creating simple application in following code first approach. have created entity model user inherits identityuser , has fields. below entity model user.

public class user : identityuser {     public int? companyid { get; set; }      public bool? canwork { get; set; }      public bool? cansearch { get; set; }      public company company { get; set; } } 

now in examples people use name applicationuser own purpose have used name user. there method in user or applicationuser model is,

public async task<claimsidentity> generateuseridentityasync(usermanager<user> manager)     {         cookieauthenticationoptions.authenticationtype         var useridentity = await manager.createidentityasync(this, defaultauthenticationtypes.applicationcookie);         // add custom user claims here         return useridentity;     } 

i unable understand purpose of method. example have used following model role,

public class role : identityrole {     public role()     {      }      public role(string rolename, string description)         : base(rolename)     {         this.description = description;     }      public string description { get; set; } } 

i understand field added unable understand purpose of overloaded constructor.

the above mentioned confusions secondary. primary confusion familiar when create entity models use dbset , dbcontext , when call entity framework method access database, database created/drop created whichever scheme following.

in identity method responsible creating identity tables in database? have identityconfig file in declare applicationusermanager , applicationsigninmanager. have startup file. had 1 startup file in app_start folder , when run application , tried accessed identity methods gave me error , not creating database. made class partial , created partial class same name @ root , exception gone , tables created. startup class responsible creating identity tables? there columns created automatically in aspnetusers phonenumber, phonenumberconfirmed, twofactorenabled. don't need these columns. can remove these? can change names of identity tables created?

i know these basic questions , not 1 question @ if unable find basic tutorial or example beginners beneficial. have found describing things don't need or making me confuse. want understand , have control how identity should work in application till seems me neither grasping , nor being able make adjustable needs. tutorials , example teaching me how make sentences unable understand alphabets. :(

first of have define model - you're doing - implementing right interfaces.
let's want create user application:

public class myuser : identityuser<string, myuserlogin, myuserrole, myuserclaim> {     public string companyname { get; set; } } 

as can see i've implemented identityuser interface (namespace microsoft.aspnet.identity.entityframework).

i've specified type of identifier want use primary key (string) , included custom objects manges login, roles , claims.

now can defined role object:

public class myrole : identityrole<string, myuserrole> { } 

again there's type , class i've defined management of users belonging to role.

public class myuserrole : identityuserrole<string> { } 

myuserlogin going implement identityuserlogin<string>.
myuserclaim going implement identityuserclaim<string>.

as can see each interface need type primary key.

the second step create user store:

public class myuserstore:  userstore<myuser, myrole, string, myuserlogin, myuserrole, myuserclaim> {     public myuserstore(mycontext context)         : base(context)     {     } } 

again have defined user, role, login etc etc want use.
need userstore cause our usermanager going need one.

if you're planning manage roles , associate roles each user have create rolestore definition.

public class myrolestore : rolestore<myrole, string, myuserrole> {     public daufrolestore(applicationdatabasecontext context) : base(context)     {     } } 

now can create usermanager. usermanager real responsible of saving changes userstore.

public class applicationusermanager : usermanager<myuser, string> {     public applicationusermanager(iuserstore<myuser, string> store)         : base(store)     {      }      public static applicationusermanager create(identityfactoryoptions<applicationusermanager> options, iowincontext context)     {         var manager = new applicationusermanager(new myuserstore(context.get<mycontext>()));          manager.uservalidator = new uservalidator<myuser, string>(manager)         {         allowonlyalphanumericusernames = false,         requireuniqueemail = true         };          manager.passwordvalidator = new passwordvalidator()         {         requiredlength = 5,         requirenonletterordigit = false,     // true         // requiredigit = true,         requirelowercase = false,         requireuppercase = false,         };          return (manager);     } } 

this class has static method create new usermanager you.
interesting note can include validation rules might need validate password etc etc.

last thing create or database context.

public class mycontext : identitydbcontext<myuser, myrole, string, myuserlogin, myuserrole, myuserclaim> {     public mycontext(): base("<your connection string here>")     {      }      public static mycontext create()     {         return new mycontext();     }      protected override void onmodelcreating(dbmodelbuilder modelbuilder)     {         base.onmodelcreating(modelbuilder);          modelbuilder.entity<myuser>()             .totable("users");          modelbuilder.entity<myrole>()             .totable("roles");          modelbuilder.entity<myuserrole>()             .totable("userroles");          modelbuilder.entity<myuserclaim>()             .totable("userclaims");          modelbuilder.entity<myuserlogin>()             .totable("userlogins");     } } 

as can see i've used model builder change names tables. can define keys or fields type or tables relations here.

this place you're going attach custom classes want manage in context:

public dbset<mycustomer> customers{ get; set; } 

again mycontext has create method returns new context:

public static mycontext create() {     return new mycontext(); } 

now should have startup class you're going bootstrap stuff:

[assembly: owinstartup(typeof(aspnetidentity2.startup))]  namespace aspnetidentity2 {     public class startup     {         public void configuration(iappbuilder app)         {             app.createperowincontext(mycontext.create);             app.createperowincontext<applicationusermanager>(applicationusermanager.create);         }     } } 

here you're going create database context , user manager can use in application.

notice first line:

[assembly: owinstartup(typeof(aspnetidentity2.startup))] 

this needed cause you're telling environment startup class needs called @ ... startup.

now in controllers can refer usermanager doing this:

httpcontext.getowincontext().getusermanager<applicationusermanager>(); 

how can create tables?

in visual studio go tools -> nuget packager manager -> package manager console.

in window there's combobox "default project". choose asp.net mvc project.
run command:

enable-migrations 

it create file configuration.cs in new folder called migrations.
if want create database need open file , change automaticmigrationsenabled true:

public configuration() {     automaticmigrationsenabled = true; } 

again, package manager console, can run:

update-database 

and tables appear in database. don't forget connection string.

you can download github project see how works.
can check these two answers other info.

the first of 2 has got links blog can learn these things.

note:

you have if want customized every single bit of environment.


Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Magento/PHP - Get phones on all members in a customer group -

session - Logging Out Using PHP -