Last active
March 7, 2023 18:06
-
-
Save ms-willfid/5773aac85537d35ca0f0d496d1e7b581 to your computer and use it in GitHub Desktop.
Asp.Net OWIN OnAuthenticationFailed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void ConfigureAuth(IAppBuilder app) | |
{ | |
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType); | |
app.UseCookieAuthentication(new CookieAuthenticationOptions()); | |
app.UseOpenIdConnectAuthentication( | |
new OpenIdConnectAuthenticationOptions | |
{ | |
ResponseType = OpenIdConnectResponseType.CodeIdToken, | |
ClientId = clientId, | |
Authority = Authority, | |
//... | |
Notifications = new OpenIdConnectAuthenticationNotifications() | |
{ | |
// If there is a code in the OpenID Connect response, redeem it for an access token | |
AuthorizationCodeReceived = (context) => | |
{ | |
// ... | |
}, | |
// On Authentication Failed | |
AuthenticationFailed = (context) => | |
{ | |
String ErrorMessage = context.Exception.Message; | |
String InnerErrorMessage = String.Empty; | |
String RedirectError = String.Format("error_message={0}", ErrorMessage); | |
if (context.Exception.InnerException != null) | |
{ | |
InnerErrorMessage = context.Exception.InnerException.Message; | |
RedirectError = String.Format("{0}&inner_error={1}", RedirectError, InnerErrorMessage); | |
} | |
// or you can just throw it | |
// throw new Exception(RedirectError); | |
RedirectError = RedirectError.Replace("\r\n", " "); | |
context.Response.Redirect("/?" + RedirectError); | |
context.HandleResponse(); | |
return Task.FromResult(0); | |
} | |
} | |
}); | |
// ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment