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

AddIdentityApiEndpoints breaks Blazor's RedirectToLogin behavior. #57900

Closed
1 task done
los93sol opened this issue Sep 16, 2024 · 8 comments
Closed
1 task done

AddIdentityApiEndpoints breaks Blazor's RedirectToLogin behavior. #57900

los93sol opened this issue Sep 16, 2024 · 8 comments
Labels
area-identity Includes: Identity and providers Docs This issue tracks updating documentation

Comments

@los93sol
Copy link

los93sol commented Sep 16, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I have a Blazor app that uses .NET Identity. The same app has a SignalR hub and I need to support both Bearer and Cookies auth so I started with a template project and configured Identity with builder.Services.AddIdentityApiEndpoints to get both schemes registered and the forward handler. This works for the most part, but it breaks Blazor's RedirectToLogin component. If you navigate to the Index page first, then click the Auth link it works properly and you are redirected to login as you would expect, but if the user has a link straight to the /auth page, they receive a 401 and are not properly routed through Blazor. This seems very unexpected as the guidance previously has been to use Bearer for SignalR, and Cookies for web interface. Is this a known thing or is there an example of how to do this properly.

Expected Behavior

No response

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

No response

Anything else?

No response

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-blazor Includes: Blazor, Razor Components label Sep 16, 2024
@javiercn javiercn added area-identity Includes: Identity and providers and removed area-blazor Includes: Blazor, Razor Components labels Sep 17, 2024
@MackinnonBuck
Copy link
Member

Thanks for reaching out, @los93sol.

Is there a reason you're not using cookie-only authentication? If your scenario is totally browser-based (i.e., there's not a native client), that's what we would recommend. We expect that this will exhibit the behavior you're expecting.

See also:

@MackinnonBuck MackinnonBuck added the Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. label Sep 17, 2024
@los93sol
Copy link
Author

I’m not using cookies only because there are remote headless workers that need to connect to the hub as well. Per the documentation Bearer is the recommended approach in this scenario.

@dotnet-policy-service dotnet-policy-service bot added Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. and removed Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. labels Sep 17, 2024
@los93sol
Copy link
Author

I added .AllowAnonymous(); at the end of MapRazorComponents() and that resolves the unexpected behavior with Blazor when components have Authorize attributes on them and allows Routes.razor to handle those as normal, but that issue seems like a massive oversight in the template and one that cost me 2 days troubleshooting.

Now the issue I’m facing is I want the API side, including SignalR to return 404 instead of 403’s and a catch all for everything that’s not a blazor route to return 401 for unauthenticated users as well. The idea here is to prevent information disclosure about endpoints that exist but users do not have access to

@mkArtakMSFT mkArtakMSFT added Docs This issue tracks updating documentation and removed Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. labels Sep 18, 2024
@mkArtakMSFT mkArtakMSFT added this to the .NET 10 Planning milestone Sep 18, 2024
@mkArtakMSFT
Copy link
Member

What you're trying to achieve should be doable. I'm going to turn this issue into a Doc item for us to improve the documentation later. In the meantime, hopefully somebody from the community will be able to help you out here.

@halter73
Copy link
Member

#9039 is related and discusses how customize OnRedirectToLogin and OnRedirectToAccessDenied for the cookie handler.

If you need more flexibility to select which handler to use for which requests/operations, you might want to use a policy scheme.

@los93sol
Copy link
Author

los93sol commented Sep 18, 2024

I was able to achieve it with the following....

Note here that I can't use the constant to get at it and the lack of an override with an action to configure it directly is what made getting at this unclear in the first place.

builder.Services.Configure<AuthenticationSchemeOptions>("Identity.BearerAndApplication", options =>
{
    // Let the OnRedirectToAccessDenied Event in ConfigureApplicationCookie handle the response for 403's
    options.ForwardForbid = IdentityConstants.ApplicationScheme;
});

builder.Services.ConfigureApplicationCookie(options =>
{
    options.Events.OnRedirectToAccessDenied = context =>
    {
        context.Response.StatusCode = StatusCodes.Status404NotFound;
        return Task.CompletedTask;
    };
});

@los93sol
Copy link
Author

The last thing I'm looking at is how an ITicketStore can be used with the BearerToken auth to move the state from the token to the server like how it can be done with Cookies. I see that BearerTokenOptions do not have an option to set it and the BearerTokenHandler does not implement it anyway so I'm looking for some method that this could be achieved as well.

@mikekistler
Copy link
Contributor

We believe that the question you've raised have been answered. If you still feel a need to continue the discussion, feel free to reopen it and add your comments.

@mikekistler mikekistler closed this as not planned Won't fix, can't repro, duplicate, stale Nov 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-identity Includes: Identity and providers Docs This issue tracks updating documentation
Projects
None yet
Development

No branches or pull requests

6 participants