Untitled

mail@pastecode.io avatar
unknown
csharp
a year ago
1.5 kB
1
Indexable
Never

// event handlers
private Task OnSecurityTokenValidated(
            SecurityTokenValidatedNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> notification)
        {
            // Parse id token and retrieve domain_hint
            var jwtHandler = new JwtSecurityTokenHandler();
            var idToken = jwtHandler.ReadJwtToken(notification.ProtocolMessage.IdToken);
            var domainHint = idToken.Claims.FirstOrDefault(claim => claim.Type == "domain_hint")?.Value;
            
            // Add domain_hint to cookies
            if (!string.IsNullOrEmpty(domainHint))
                notification.Response.Cookies.Append("domain_hint", domainHint);

            return Task.FromResult(0);
        }

        private Task OnRedirectToIdentityProvider(
                RedirectToIdentityProviderNotification<OpenIdConnectMessage, OpenIdConnectAuthenticationOptions> notification)
        {
            // Use domain_hint from cookies to skip identity provider selection screen
            var cookies = notification.Request.Cookies;
            var domainHint = cookies.FirstOrDefault(x => x.Key == "domain_hint").Value;
            if (domainHint != null)
                notification.ProtocolMessage.DomainHint = domainHint;
            return Task.FromResult(0);
        }

// cookie manager
CookieManager = new SystemWebCookieManager()


// ASP.NET web host compatible cookie manager
                CookieManager = new SystemWebChunkingCookieManager()