public async Task <IHttpActionResult> SignIn(LoginViewModel model) { if (!ModelState.IsValid) { return(BadRequest()); } var user = await _userManager.FindAsync(model.UserName, model.Password); if (user == null) { return(BadRequest()); } Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie); ClaimsIdentity oAuthIdentity = await _userManager.CreateIdentityAsync(user, OAuthDefaults.AuthenticationType); ClaimsIdentity cookieIdentity = await _userManager.CreateIdentityAsync(user, CookieAuthenticationDefaults.AuthenticationType); AuthenticationProperties properties = await CreateInitialRefreshToken(model.ClientId, user, oAuthIdentity); properties.IsPersistent = model.IsPersistance; Authentication.SignIn(properties, oAuthIdentity, cookieIdentity); await _userManager.AddLoginAsync(user.Id, new UserLoginInfo(AuthenticationConstants.InternalLoginProvider, user.Id)); return(Ok()); }
public async Task <IdentityResult> CreateNewUser(ApplicationUser user, string password, string requestedOrganization) { var userSettings = _organizationDbSet.Where(o => o.ShortName == requestedOrganization) .Select(u => new { u.CultureCode, u.TimeZone }) .First(); user.OrganizationId = _organizationService.GetOrganizationByName(requestedOrganization).Id; user.EmploymentDate = DateTime.UtcNow; user.CultureCode = userSettings.CultureCode ?? ConstBusinessLayer.DefaultCulture; user.TimeZone = userSettings.TimeZone; user.NotificationsSettings = null; var result = await _userManager.CreateAsync(user, password); if (!result.Succeeded) { return(result); } var userLoginInfo = new UserLoginInfo(AuthenticationConstants.InternalLoginProvider, user.Id); var addLoginResult = await _userManager.AddLoginAsync(user.Id, userLoginInfo); if (!addLoginResult.Succeeded) { return(addLoginResult); } AddNewUserRoles(user.Id); await SendUserVerificationEmail(user, requestedOrganization); return(result); }