// Configure Auth0 parameters string auth0Domain = SettingsManager.GetSettingValue(AppSettings.System.Auth0.Auth0Domain); string auth0ClientId = SettingsManager.GetSettingValue(AppSettings.System.Auth0.Auth0ClientId_Web); string auth0ClientSecret = SettingsManager.GetSettingValue(AppSettings.System.Auth0.Auth0ClientSecret_Web); string auth0RedirectUri = SettingsManager.GetSettingValue(AppSettings.System.Auth0.Auth0RedirectUri); string auth0PostLogoutRedirectUri = SettingsManager.GetSettingValue(AppSettings.System.Auth0.Auth0PostLogoutRedirectUri); // Enable the Cookie saver middleware to work around a bug in the OWIN implementation app.UseKentorOwinCookieSaver(); // Set Cookies as default authentication type app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType); app.UseCookieAuthentication(new CookieAuthenticationOptions { CookieName = "PremierChequeAuth", AuthenticationType = CookieAuthenticationDefaults.AuthenticationType, LoginPath = new PathString("/Account/Login"), ExpireTimeSpan = TimeSpan.FromMinutes(60) }); // Configure Auth0 authentication for website login app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions { AuthenticationType = "Auth0", Authority = $"https://{auth0Domain}", ClientId = auth0ClientId, ClientSecret = auth0ClientSecret, RedirectUri = auth0RedirectUri, PostLogoutRedirectUri = auth0PostLogoutRedirectUri, ResponseType = OpenIdConnectResponseType.CodeIdToken, Scope = "openid profile", TokenValidationParameters = new TokenValidationParameters { NameClaimType = "name" }, Notifications = new OpenIdConnectAuthenticationNotifications { RedirectToIdentityProvider = notification => { if (notification.ProtocolMessage.RequestType == OpenIdConnectRequestType.Logout) { var logoutUri = $"https://{auth0Domain}/v2/logout?client_id={auth0ClientId}"; var postLogoutUri = notification.ProtocolMessage.PostLogoutRedirectUri; if (!string.IsNullOrEmpty(postLogoutUri)) { if (postLogoutUri.StartsWith("/")) { // transform to absolute var request = notification.Request; postLogoutUri = request.Scheme + "://" + request.Host + request.PathBase + postLogoutUri; } logoutUri += $"&returnTo={ Uri.EscapeDataString(postLogoutUri)}"; } notification.Response.Redirect(logoutUri); notification.HandleResponse(); } return Task.FromResult(0); } } });