c# - ASP.NET Assigning default user roles upon registration -
how can assign default roles e.g. standard user upon user registration?
at moment, can manually assign roles editing table in sql server there way default role can assigned users?
i've tried using solution posted here doesn't appear work projects using asp.net core web application (visual studio 2015).
// post: /account/register [httppost] [allowanonymous] [validateantiforgerytoken] public async task<iactionresult> register(registerviewmodel model, string returnurl = null) { viewdata["returnurl"] = returnurl; if (modelstate.isvalid) { var user = new applicationuser { username = model.email, email = model.email }; var result = await _usermanager.createasync(user, model.password); if (result.succeeded) { // more information on how enable account confirmation , password reset please visit http://go.microsoft.com/fwlink/?linkid=532713 // send email link //var code = await _usermanager.generateemailconfirmationtokenasync(user); //var callbackurl = url.action("confirmemail", "account", new { userid = user.id, code = code }, protocol: httpcontext.request.scheme); //await _emailsender.sendemailasync(model.email, "confirm account", // $"please confirm account clicking link: <a href='{callbackurl}'>link</a>"); await _signinmanager.signinasync(user, ispersistent: false); _logger.loginformation(3, "user created new account password."); return redirecttolocal(returnurl); } adderrors(result); } // if got far, failed, redisplay form return view(model); }
i assume use default asp .net membership , role providers.
if (result.succeeded) { roles.addusertorole(user.username, "<role>"); await _signinmanager.signinasync(user, ispersistent: false); _logger.loginformation(3, "user created new account password."); return redirecttolocal(returnurl); }
above code helps make working.
Comments
Post a Comment