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 <IHttpActionResult> GetExternalLogin(string provider, string client_Id = null, string userId = null, bool isRegistration = false, string error = null) { if (string.IsNullOrEmpty(client_Id) || error != null) { var uri = CreateErrorUri("error"); return(Redirect(uri)); } if (!User.Identity.IsAuthenticated) { return(new ChallengeResult(provider, this)); } ExternalLoginData externalLogin = ExternalLoginData.FromIdentity(User.Identity as ClaimsIdentity); if (externalLogin.Email == null) { var uri = CreateErrorUri("emailError"); Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie); return(Redirect(uri)); } if (externalLogin.LoginProvider != provider) { Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie); return(new ChallengeResult(provider, this)); } var user = await _userManager.FindAsync(new UserLoginInfo(externalLogin.LoginProvider, externalLogin.ProviderKey)); var hasLogin = user != null; if (isRegistration == true && hasLogin == false) { var isEmailHostValid = _organizationService.IsOrganizationHostValid(externalLogin.Email, RequestedOrganization); if (!isEmailHostValid) { var uri = CreateErrorUri("error"); Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie); return(Redirect(uri)); } } // Linking accounts. if (userId != null) { return(await LinkAccounts(externalLogin, userId)); } // Registration process. if (isRegistration == true) { return(await RegisterOrLogin(user, externalLogin, client_Id, hasLogin)); } // Login process. return(await Login(user, externalLogin, client_Id, hasLogin)); }