asp.net - Custom and unique field for ApplicationUser -


i creating register form on application. have field must unique , have created custom validation ensure this. when submit form unique values page not change or submit anything. far viewmodel looks this

    public class registerviewmodel  {     [required]     [customvalidation(accountcontroller, "checkifusernametaken", errormessage = "in use already")]     [display(name = "username")]     [maxlength(15, errormessage = "this field must less 15 characters in length"), minlength(2, errormessage = "this field must have value greater 2")]     public string usernameidentity { get; set; }      [required]     [display(name = "first name")]     [maxlength(15, errormessage = "this field must less 15 characters in length"), minlength(2, errormessage = "this field must have value greater 2")]     public string firstname { get; set; }      [required]     [emailaddress]     [display(name = "email")]     public string email { get; set; }      [required]     [stringlength(100, errormessage = "the {0} must @ least {2} characters long.", minimumlength = 6)]     [datatype(datatype.password)]     [display(name = "password")]     public string password { get; set; }      [datatype(datatype.password)]     [display(name = "confirm password")]     [compare("password", errormessage = "the password , confirmation password not match.")]     public string confirmpassword { get; set; } } 

in controller have custom validation so:

    public jsonresult usernameinuse(string usernamein)     {         return json(!context.users.any(u => u.usernameidentity == usernamein), jsonrequestbehavior.allowget);     } 

and asynchronous register method in same controller:

        // post: /account/register     [httppost]     [allowanonymous]     [validateantiforgerytoken]     public async task<actionresult> register(registerviewmodel model)     {         if (modelstate.isvalid)         {             var user = new applicationuser             {                 usernameidentity = model.usernameidentity,                 username = model.email,                 email = model.email,                 firstname = model.firstname             };             var result = await usermanager.createasync(user, model.password);               if (result.succeeded)             {                 // add registering users default user role                 usermanager.addtorole(user.id, "user");                  await signinmanager.signinasync(user, ispersistent:false, rememberbrowser:false);                 return redirecttoaction("index", "home");             }             adderrors(result);         }          // if got far, failed, redisplay form         return view(model);     }    @model project.registerviewmodel @{     viewbag.title = "register"; }  <h2>@viewbag.title.</h2>  @using (html.beginform("register", "account", formmethod.post, new { @class = "form-horizontal", role = "form" })) {     @html.antiforgerytoken()     <h4>create new account.</h4>     <hr />     @html.validationsummary("", new { @class = "text-danger" })     <div class="form-group">         @html.labelfor(m => m.firstname, new { @class = "col-md-2 control-label" })         <div class="col-md-10">             @html.textboxfor(m => m.firstname, new { @class = "form-control" })         </div>     </div>     <div class="form-group">         @html.labelfor(m => m.usernameidentity, new { @class = "col-md-2 control-label" })         <div class="col-md-10">             @html.textboxfor(m => m.usernameidentity, new { @class = "form-control" })         </div>     </div>     <div class="form-group">         @html.labelfor(m => m.email, new { @class = "col-md-2 control-label" })         <div class="col-md-10">             @html.textboxfor(m => m.email, new { @class = "form-control" })         </div>     </div>     <div class="form-group">         @html.labelfor(m => m.password, new { @class = "col-md-2 control-label" })         <div class="col-md-10">             @html.passwordfor(m => m.password, new { @class = "form-control" })         </div>     </div>     <div class="form-group">         @html.labelfor(m => m.confirmpassword, new { @class = "col-md-2 control-label" })         <div class="col-md-10">             @html.passwordfor(m => m.confirmpassword, new { @class = "form-control" })         </div>     </div>     <div class="form-group">         <div class="col-md-offset-2 col-md-10">             <input type="submit" class="btn btn-default" value="register" />         </div>     </div> }  @section scripts {     @scripts.render("~/bundles/jqueryval") } 

create custom validationattribute check inputed username:

public class uniqueusernameattribute : validationattribute {     protected override validationresult isvalid(object value, validationcontext validationcontext)     {         // object         var username = value string;          // check if exists         dbcontext context = new dbcontext();         var user = context.users.firstordefault(u => u.usernameidentity == username)          if (user == null)             return validationresult.success;         else             return new validationresult("username exists");     } } 

then can use it:

[required] [uniqueusernameattribute] [display(name = "username")] [maxlength(15, errormessage = "this field must less 15 characters in length"), minlength(2, errormessage = "this field must have value greater 2")] public string usernameidentity { get; set; } 

Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -