c# - How to extend IdentityUser with custom property -


i'm using asp.net identity 2.0 users log website, authentication details stored in sql database. asp.net identity has been implemented in standard way can found in many online tutorials.

the applicationuser class in identitymodels has been extended include custom property:

public class applicationuser : identityuser {     public async task<claimsidentity> generateuseridentityasync(usermanager<applicationuser> manager, string authenticationtype)     {        cookieauthenticationoptions.authenticationtype        var useridentity = await manager.createidentityasync(this, authenticationtype);        return useridentity;     }     //my extended property     public string code { get; set; } } 

when register new user pass code custom property in registerbindingmodel i'm not sure how insert custom property webusers table.

i did bellow doesn't inserting property table username , password.

var user = new applicationuser() { username = username, email = model.email, code=model.code }; 

and entire function:

[allowanonymous] [route("register")] public async task<ihttpactionresult> register(registerbindingmodel model)     {         if (!modelstate.isvalid)         {             return badrequest(modelstate);         }          var username = !string.isnullorempty(model.username) ? model.username : model.email;         //i set here doesn't inserted table.         var user = new applicationuser() { username = username, email = model.email, code=model.code };          identityresult result = await usermanager.createasync(user, model.password);          if (!result.succeeded)         {             return geterrorresult(result);         }          return ok();     } 

what missing? looking @ similar questions couldn't find answer this.

if follow steps of adding custom field user, finish tasks successfully.

here steps add custom field user:

  1. create asp.net web application
  2. make sure select mvc , authentication individual user accounts
  3. go models folder → open identitymodels.csapplicationuser class , add property:

    public string code { get; set; } 
  4. build project
  5. go tools menu → nuget package manager → click package manager console
  6. type enable-migrations , press enter , wait until task completed. see response says:

       checking if context targets existing database...    code first migrations enabled project webapplication1. 
  7. type add-migration "code" , press enter , wait until task completed. see response says:

    scaffolding migration 'code'. designer code migration file includes snapshot of current code first model. snapshot used calculate changes model when scaffold next migration. if make additional changes model want include in migration, can re-scaffold running 'add-migration code' again. 
  8. type update-database , press enter , wait until task completed. see response says:

    specify '-verbose' flag view sql statements being applied  target database. applying explicit migrations: [201611132135242_code]. applying explicit migration: 201611132135242_code. running seed method. 

    at step if refresh sql server object explorer , go database , see tables, under dbo.aspnetusers under columns, see code field. if didn't know database or server should for, open web.config file , take @ connection string this:

    <add name="defaultconnection" connectionstring="data source=(localdb)\v11.0;attachdbfilename=|datadirectory|\aspnet-webapplication1-20161114125903.mdf;initial catalog=aspnet-webapplication1-20161114125903;integrated security=true" providername="system.data.sqlclient" /> 

    you can see data source (which sql server instance) , .mdf database name.

  9. go models folder → open accountviewmodels.cs file → registerviewmodel class , add property:

    public string code { get; set; } 
  10. go views folder → account folder → open register.cshtml file , add code near other fields, example below password:

    <div class="form-group">     @html.labelfor(m => m.code, new { @class = "col-md-2 control-label" })     <div class="col-md-10">         @html.textboxfor(m => m.code, new { @class = "form-control" })     </div> </div> 
  11. go controllers folder → open accountcontroller.cs file → in http post register action, change line creates user this:

    var user = new applicationuser { username = model.email, email = model.email,     code= model.code }; 
  12. run project , go /account/register url , register new user. after registering user, if go database again , view data of dbo.aspnetusers table, see code has been saved.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -