-
-
Save PascalSenn/a3204e7bb880705a178048a43ee2147c to your computer and use it in GitHub Desktop.
Authentication Provider
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
services.AddAuthentication(options => | |
{ | |
options.DefaultScheme = "YOURSCHEMANAME"; | |
options.DefaultAuthenticateScheme = "YOURSCHEMANAME"; | |
}) | |
.AddJwtBearer("Websockets", ctx => { }) | |
.AddIdentityServerAuthentication("YOURSCHEMANAME", options => | |
{ | |
options.ForwardDefaultSelector = context => | |
{ | |
if (!context.Items.ContainsKey(AuthenticationSocketInterceptor.HTTP_CONTEXT_WEBSOCKET_AUTH_KEY) && | |
context.Request.Headers.TryGetValue("Upgrade", out var value) && | |
value.Count > 0 && | |
value[0] is string stringValue && | |
stringValue == "websocket") | |
{ | |
return "Websockets"; | |
} | |
return "YOURSCHEMANAME"; | |
}; | |
options.TokenRetriever = new Func<HttpRequest, string>(req => | |
{ | |
if (req.HttpContext.Items.TryGetValue( | |
AuthenticationSocketInterceptor.HTTP_CONTEXT_WEBSOCKET_AUTH_KEY, | |
out object token) && | |
token is string stringToken) | |
{ | |
return stringToken; | |
} | |
var fromHeader = TokenRetrieval.FromAuthorizationHeader(); | |
var fromQuery = TokenRetrieval.FromQueryString(); // Query string auth | |
return fromHeader(req) ?? fromQuery(req); | |
}); | |
options.JwtBearerEvents = new JwtBearerEvents() | |
{ | |
OnTokenValidated = context => | |
{ | |
// Token validation | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment