public IHttpActionResult Record(AngularExceptionModel model) { // Create the exception and exception context var exception = new AngularException(model.ToString()); var catchBlock = new ExceptionContextCatchBlock("catchBlock", true, false); var context = new ExceptionContext(exception, catchBlock, Request); var loggerContext = new ExceptionLoggerContext(context); // Call elmah & log the exception var logger = new ExceptionHandling.ElmahExceptionLogger(); logger.Log(loggerContext); // Return return Ok(); }
public IHttpActionResult Record(AngularExceptionModel model) { // Create the exception and exception context var exception = new AngularException(model.ToString()); var catchBlock = new ExceptionContextCatchBlock("catchBlock", true, false); var context = new ExceptionContext(exception, catchBlock, Request); var loggerContext = new ExceptionLoggerContext(context); // Call elmah & log the exception var logger = new ExceptionHandling.ElmahExceptionLogger(); logger.Log(loggerContext); // Return return(Ok()); }
public IHttpActionResult Record(ClientExceptionModel model) { // Create the exception and exception context var exception = new ClientException(model); var catchBlock = new ExceptionContextCatchBlock("catchBlock", true, false); var context = new ExceptionContext(exception, catchBlock, Request); var loggerContext = new ExceptionLoggerContext(context); // Call elmah & log the exception var logger = new ExceptionHandling.ElmahExceptionLogger(); logger.Log(loggerContext); // Return return(StatusCode(HttpStatusCode.NoContent)); }
public async Task <IHttpActionResult> ExternalLoginCallback(string clientReturnUrl, string error = null) { // Error message from the provider, pass it on if (!string.IsNullOrWhiteSpace(error)) { return(Redirect(string.Format("{0};error={1}", clientReturnUrl, error))); } // Since this method MUST return RedirectResult, cover the whole block with try & catch, // so it always redirect the user back to ngClient and won't get stuck on WebApi in case of an error // coni2k - 16 Jan. '16 try { var content = await GetLoginInfoText(); var externalLoginInfo = await Authentication.GetExternalLoginInfoAsync(); // We will switch to local account, sign out from the external Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie); // Validate external login info if (externalLoginInfo == null) { // User canceled the operation? return(Redirect(string.Format("{0};error={1}", clientReturnUrl, "Login failed, please try again"))); } // Validate email var email = externalLoginInfo.Email; if (string.IsNullOrWhiteSpace(email)) { // User didn't give permission for email address return(Redirect(string.Format("{0};error={1}", clientReturnUrl, "Login failed, please give permission to access your email address"))); } var user = await UserManager.FindAsync(externalLoginInfo.Login); // There is no externalLogin with these info if (user == null) { user = await UserManager.FindByEmailAsync(externalLoginInfo.Email); // And there is no user with this email address: New user if (user == null) { var userName = await UserManager.GetUniqueUserNameFromEmail(email); user = new User(userName, email); var result = await UserManager.CreateAsync(user, externalLoginInfo.Login); var errorMessage = GetErrorMessage(result); if (errorMessage != null) { return(Redirect(string.Format("{0};error={1}", clientReturnUrl, errorMessage))); } // init=true: to let the client knows that this account is newly created (first login from this external login) clientReturnUrl = string.Format("{0};init=true", clientReturnUrl); } else // There is a user with this email: Link accounts { var result = await UserManager.LinkLoginAsync(user, externalLoginInfo.Login); var errorMessage = GetErrorMessage(result); if (errorMessage != null) { return(Redirect(string.Format("{0};error={1}", clientReturnUrl, errorMessage))); } } } else // Existing user { // If the email address has changed meanwhile if (user.Email != email) { user.Email = email; } await UserManager.AddSingleUseTokenAsync(user); } // Redirect return(Redirect(string.Format("{0};token={1}", clientReturnUrl, user.SingleUseToken))); } catch (Exception ex) { // Log the exception with Elmah var logger = new ExceptionHandling.ElmahExceptionLogger(); logger.Log(ex, Request, "AccountController.ExternalLoginCallback"); // Redirect the user back to the client return(Redirect(string.Format("{0};error={1}", clientReturnUrl, "Login failed, please try again later"))); } }
public async Task<IHttpActionResult> ExternalLoginCallback(string error = null) { var clientAppUrl = Framework.AppSettings.ClientAppUrl; var location = string.Format("{0}/account/login", clientAppUrl); // Error message from the provider, pass it on if (!string.IsNullOrWhiteSpace(error)) { return Redirect(string.Format("{0}?error={1}", location, error)); } // Since this method MUST return RedirectResult, cover the whole block with try & catch, // so it always redirect the user back to ngClient and won't get stuck on WebApi // SH - 16 Jan. '16 try { var content = await GetLoginInfoText(); var externalLoginInfo = await Authentication.GetExternalLoginInfoAsync(); // We will switch to local account, sign out from the external Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie); // Validate external login info if (externalLoginInfo == null) { // User canceled the operation? return Redirect(string.Format("{0}?error={1}", location, "Login failed, please try again")); } // Validate email var email = externalLoginInfo.Email; if (string.IsNullOrWhiteSpace(email)) { // User didn't give permission for email address return Redirect(string.Format("{0}?error={1}", location, "Login failed, please give permission to access your email address")); } var tempToken = string.Empty; var user = await UserManager.FindAsync(externalLoginInfo.Login); // There is no externalLogin with these info if (user == null) { user = await UserManager.FindByEmailAsync(externalLoginInfo.Email); // And there is no user with this email address: New user if (user == null) { user = new User(email); var result = await UserManager.CreateAsync(user, externalLoginInfo.Login); var errorMessage = GetErrorMessage(result); if (errorMessage != null) { return Redirect(string.Format("{0}?error={1}", location, errorMessage)); } } else // There is a user with this email: Link accounts { var result = await UserManager.LinkLoginAsync(user, externalLoginInfo.Login); var errorMessage = GetErrorMessage(result); if (errorMessage != null) { return Redirect(string.Format("{0}?error={1}", location, errorMessage)); } } } else // Existing user { // If the email address has changed meanwhile if (user.Email != email) user.Email = email; await UserManager.AddTempTokenClaimAsync(user); } // Get the temp token tempToken = user.Claims.OrderByDescending(claim => claim.CreatedOn).First(claim => claim.ClaimType == "TempToken").ClaimValue; // Redirect location = string.Format("{0}/account/externalLogin?tempToken={1}", clientAppUrl, tempToken); return Redirect(location); } catch (Exception ex) { // Log the exception with Elmah var logger = new ExceptionHandling.ElmahExceptionLogger(); logger.Log(ex, Request, "AccountController.ExternalLoginCallback"); // Redirect the user back to the client return Redirect(string.Format("{0}?error={1}", location, "Login failed, please try again later")); } }