public async Task <ActionResult> ExternalLoginCallback(string returnUrl) { var ctx = Request.GetOwinContext(); var result = await ctx.Authentication.AuthenticateAsync("ExternalCookie"); ctx.Authentication.SignOut("ExternalCookie"); var externalLogin = ExternalLoginModel.FromIdentity(result.Identity); if (externalLogin == null) { return(new HttpStatusCodeResult(500)); } var claims = result.Identity.Claims.ToList(); claims.Add(new Claim(ClaimTypes.AuthenticationMethod, "Google")); var ci = new ClaimsIdentity(claims, "Cookie"); ctx.Authentication.SignIn(ci); Session["ExternalLoginModel"] = externalLogin; if (!String.IsNullOrEmpty(returnUrl)) { return(new RedirectResult(returnUrl)); } return(RedirectToAction("Index", "Dashboard", new { area = "Account" })); }
public async Task <IHttpActionResult> GetExternalLogin(string provider, string error = null) { if (error != null) { return(Redirect(Url.Content("~/") + "#error=" + Uri.EscapeDataString(error))); } ExternalLoginProvider loginProvider; if (!Enum.TryParse <ExternalLoginProvider>(provider, ignoreCase: true, result: out loginProvider) || loginProvider == ExternalLoginProvider.None) { //Unsupported login provider return(InternalServerError()); } if (!User.Identity.IsAuthenticated) { return(new ChallengeResult(loginProvider, this)); } ExternalLoginModel externalLogin = ExternalLoginModel.FromIdentity(User.Identity as ClaimsIdentity); if (externalLogin == null) { return(InternalServerError()); } if (externalLogin.Provider != loginProvider) { Request.GetOwinContext().Authentication.SignOut( DefaultAuthenticationTypes.ExternalCookie, OAuthDefaults.AuthenticationType, CookieAuthenticationDefaults.AuthenticationType); return(new ChallengeResult(loginProvider, this)); } User user = await this.UserProvider.FindAsync(externalLogin.Provider, externalLogin.ProviderKey); if (user != null) { OwinHelper.SingIn(Request.GetOwinContext(), user, externalLogin); } else { OwinHelper.SingIn(Request.GetOwinContext(), externalLogin); } return(Ok()); }
public async Task <IHttpActionResult> ExternalLoginCallback(string provider) { _logger.Debug(string.Format("ini ExternalLoginCallback process - provider:{0}", provider)); string redirectUri = string.Empty; if (!User.Identity.IsAuthenticated) { _logger.Debug("ExternalLoginCallback IsAuthenticated"); return(new ChallengeResult(provider, this)); } var redirectUriValidationResult = Helpers.ValidateClientAndRedirectUri(ref redirectUri, Request); if (!string.IsNullOrWhiteSpace(redirectUriValidationResult)) { _logger.Debug("ExternalLoginCallback BadRequest"); return(BadRequest(redirectUriValidationResult)); } ExternalLoginModel externalLogin = ExternalLoginModel.FromIdentity(User.Identity as ClaimsIdentity); if (externalLogin == null) { _logger.Debug("ExternalLoginCallback InternalServerError"); return(InternalServerError()); } if (externalLogin.LoginProvider != provider) { _logger.Debug("ExternalLoginCallback SignOut different providers"); Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie); return(new ChallengeResult(provider, this)); } User user = await _appUserManager.FindAsync(new UserLoginInfo(externalLogin.LoginProvider, externalLogin.ProviderKey)); bool hasRegistered = user != null; if (hasRegistered && user.Disabled) { return(BadRequest("Your account is disabled, please contact with the web master")); } redirectUri = string.Format("{0}?externalAccessToken={1}&provider={2}&haslocalaccount={3}&userName={4}&email={5}", redirectUri, externalLogin.ExternalAccessToken, externalLogin.LoginProvider, hasRegistered, externalLogin.UserName, externalLogin.Email); _logger.Debug(string.Format("ExternalLoginCallback Redirect info provider:{0},hasRegistered:{1},externalLogin.UserName:{2},externalLogin.Email{3}", provider, hasRegistered, externalLogin.UserName, externalLogin.Email)); return(Redirect(redirectUri)); }
public UserViewModel GetUser() { ClaimsIdentity userIdentity = User.Identity as ClaimsIdentity; ExternalLoginModel externalLogin = ExternalLoginModel.FromIdentity(userIdentity); var user = new UserViewModel { Email = userIdentity.FindFirstValue(ClaimTypes.Email), FullName = userIdentity.FindFirstValue(ClaimTypes.GivenName), IsVerified = Boolean.Parse(userIdentity.FindFirstValue(OwinHelper.ClaimTypeIsVerified)), AvatarUrl = userIdentity.FindFirstValue(OwinHelper.ClaimTypeAvatarUrl), IsRegistered = (externalLogin == null || externalLogin.IsRegistered), LoginProvider = (externalLogin != null ? externalLogin.Provider.ToString() : null) }; return(user); }
public async Task <RegistrationResult> RegisterExternal() { ExternalLoginModel externalLogin = ExternalLoginModel.FromIdentity(User.Identity as ClaimsIdentity); if (externalLogin == null) { throw new ApiException(Exceptions.ExternalLoginNotFound); } var user = await this.UserProvider.CreateExternalAsync(externalLogin); var userViewModel = UserProvider.MapUserToViewModel(user, externalLogin); OwinHelper.SingIn(Request.GetOwinContext(), user, externalLogin); var token = OwinHelper.CreateToken(Request.GetOwinContext(), user, externalLogin); return(new RegistrationResult(userViewModel, token)); }