Skip to content

Instantly share code, notes, and snippets.

@PascalSenn
Created March 12, 2020 20:57
Show Gist options
  • Save PascalSenn/a3204e7bb880705a178048a43ee2147c to your computer and use it in GitHub Desktop.
Save PascalSenn/a3204e7bb880705a178048a43ee2147c to your computer and use it in GitHub Desktop.
Authentication Provider
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