Skip to content

Instantly share code, notes, and snippets.

@ms-willfid
Last active March 7, 2023 18:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ms-willfid/5773aac85537d35ca0f0d496d1e7b581 to your computer and use it in GitHub Desktop.
Save ms-willfid/5773aac85537d35ca0f0d496d1e7b581 to your computer and use it in GitHub Desktop.
Asp.Net OWIN OnAuthenticationFailed
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