Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IHttpContextAccessor context always null with Azure SignalR Service #17617

Closed
Zenuka opened this issue Dec 5, 2019 · 4 comments
Closed

IHttpContextAccessor context always null with Azure SignalR Service #17617

Zenuka opened this issue Dec 5, 2019 · 4 comments
Labels
area-signalr Includes: SignalR clients and servers

Comments

@Zenuka
Copy link

Zenuka commented Dec 5, 2019

This feature request is the same as #12535 but unfortunately it's locked (just a few days ago) and I can't comment on it. For security purposes I would like to use the access token in the request (because it's not available in the claims) to get the features a user has access to from another API. The access token is probably not available in the claims because I have another workaround in place from: IdentityServer/IdentityServer4#2349 (comment)

To get the access token, locally (without Azure SignalR service) I use the IHttpContextAccessor to access the HttpContext.Request and get the token but this is not available when using the Azure SignalR service. Is it possible to allow to access the HttpContext from the IHttpContextAccessor when using Azure SignalR service?

@javiercn javiercn added the area-signalr Includes: SignalR clients and servers label Dec 5, 2019
@halter73
Copy link
Member

halter73 commented Dec 5, 2019

Since the SignalR connection is routed through the Azure SignalR service instead of there being a direct connection to the ASP.NET Core server, there isn't any HttpContext to access. Is there any reason using HubCallerContext.User as suggested in #12535 (comment) doesn't address your use case?

@Zenuka
Copy link
Author

Zenuka commented Dec 5, 2019

I need to access the access_token to ask another API for the rights this user had based on the claims but the access token is not available in claims. Probably of the other workaround I linked in the original post.

@BrennanConroy
Copy link
Member

We generally recommend that you grab the access_token and put it in your claims in middleware before SignalR runs.

@Zenuka
Copy link
Author

Zenuka commented Dec 6, 2019

Thanks for both your quick replies! I've spent my morning trying to store the access token as a claim again and seems I was making a stupid (and unrelated) mistake. I was using JwtBearerEvents.OnTokenValidated but this method was never called. That's because I use token introspection so I should have used OAuth2IntrospectionEvents.OnTokenValidated instead. Now I got the access token as a claim and I can access it in my AuthorizationHandlers using AuthorizationHandlerContext.User.

Just for when someone hits this issue from google, the important pieces of my solution:

services.AddAuthentication("Bearer")
        .AddIdentityServerAuthentication("Bearer",
            options =>
            {
                options.Authority = "...";
                options.ApiName = "...";
                options.ApiSecret = "..."; // Do not use 'options.JwtBearerEvents' when using introspection 
                options.TokenRetriever = CustomTokenRetriever.FromHeaderAndQueryString; // Src: https://github.com/IdentityServer/IdentityServer4/issues/2349#issuecomment-394099795
                options.OAuth2IntrospectionEvents = new OAuth2IntrospectionEvents
                {
                    OnTokenValidated = context =>
                    {
                        if (!string.IsNullOrWhiteSpace(context.SecurityToken) && context.Principal.Identity is ClaimsIdentity identity && !identity.HasClaim(c => c.Type == "access_token"))
                        {
                            identity.AddClaim(new Claim("access_token", context.SecurityToken));
                        }

                        return Task.CompletedTask;
                    }
                };
                
            });

@Zenuka Zenuka closed this as completed Dec 6, 2019
@ghost ghost locked as resolved and limited conversation to collaborators Jan 5, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-signalr Includes: SignalR clients and servers
Projects
None yet
Development

No branches or pull requests

4 participants