public HttpResponseMessage Authenticate([FromBody] UserProfile userProfile) { LoginDo loginDO = new LoginDo(); HttpResponseMessage response; try { userProfile = loginDO.Authenticate(userProfile); if (userProfile.IsAuthenticated) { UserRole userRole = loginDO.GetUserRole(userProfile.UserName); if (userRole != null) { userProfile.UserRole = userRole; } response = Request.CreateResponse <UserProfile>(HttpStatusCode.OK, userProfile); } else { response = Request.CreateResponse <UserProfile>(HttpStatusCode.Unauthorized, userProfile); } } catch (Exception ex) { response = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message); } return(response); }
public async Task <UserDo> Login([FromBody] LoginDo model) { var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, false, false); if (result.Succeeded) { var appUser = _userManager.Users.SingleOrDefault(r => r.Email == model.Email); return(new UserDo { Id = appUser.Id, Email = model.Email, Token = GenerateJwtToken(model.Email, appUser).ToString() }); } throw new ApplicationException("Invalid login attempt"); }
public async Task <object> Register([FromBody] LoginDo model) { var user = new IdentityUser { UserName = model.Email, Email = model.Email }; var result = await _userManager.CreateAsync(user, model.Password); if (result.Succeeded) { await _signInManager.SignInAsync(user, false); return(GenerateJwtToken(model.Email, user)); } throw new ApplicationException(); }