c# - How to extend Identity in Net Core Web API -
hi i'm using entityframework core , want use built in identity authentication net core web api, why did following:
-create class extends identityuser, , add custom properties :
public class myuser : identityuser { public myuser(string username) : base (username) { } public string firstname { set; get; } public string lastname { set; get; } public datetime birthdate { set; get; } } create db context class inherits identitydbcontext :
public class dbcontext : identitydbcontext<myuser> { public dbcontext (dbcontextoptions<dbcontext> options) : base (options) { } public dbset<story> stories { set; get; } protected override void onmodelcreating (modelbuilder builder) { base.onmodelcreating (builder); } } then registered custom class , db context in startup.cs(in configureservices method):
public void configureservices (iservicecollection services) { var connection = @"server=(localdb)\mssqllocaldb;database=dbef;trusted_connection=true;"; services.adddbcontext<dbcontext> (options => options.usesqlserver (connection, optionsbuilder => optionsbuilder.migrationsassembly ("webapiefcore"))); services.addidentity<myuser, identityrole> () .addentityframeworkstores<dbcontext> () .adddefaulttokenproviders (); services.configure<identityoptions> (o => { o.signin.requireconfirmedemail = true; }); services.addmvc (); } finally enabled migrations , update db can see here: 
the db identity tables plus custom 1 created.
looking on internet found version of identity using asp.net web api, seems need override method in custom user class, properties of class saved.
i followed tutorial implement identity in web api, of tutorials use identity mvc not pure web api: tutorial followed
my question is:
how create controller post action can register users. , need modify in custom user class can save custom properties in db.
also when register user has have properties class: firstname, lastname , birthday. meant cannot null , have filled, if empty cannot save db.
thanks
have @ samples in their github repository:
for instance post in controller, can @ register method (the method you're looking createasync method on usermanager object)
Comments
Post a Comment