svetlai

Register With Role

Jan 24th, 2016
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.77 KB | None | 0 0
  1. // POST api/Account/Register
  2.         [AllowAnonymous]
  3.         [Route("Register")]
  4.         public async Task<IHttpActionResult> Register(RegisterBindingModel model)
  5.         {
  6.             if (!ModelState.IsValid)
  7.             {
  8.                 return BadRequest(ModelState);
  9.             }
  10.  
  11.             var user = new User() { UserName = model.Email, Email = model.Email, ProfileType = (ProfileType)Enum.Parse(typeof(ProfileType), model.ProfileType) };
  12.  
  13.             IdentityResult result = await UserManager.CreateAsync(user, model.Password);
  14.  
  15.             if (!result.Succeeded)
  16.             {
  17.                 return GetErrorResult(result);
  18.             }
  19.             else
  20.             {
  21.                 UserManager.AddToRole(user.Id, "User");
  22.             }
  23.  
  24.             return Ok();
  25.         }
Add Comment
Please, Sign In to add comment