-
Notifications
You must be signed in to change notification settings - Fork 597
Discussion for Auth 2.0 #1338
Comments
Hi, In the announcement for the breaking changes you said: "In 1.0, it was possible to configure different Authentication middleware with branching, this is no longer possible with a single middleware and shared services across all branches. A workaround could be to use different schemes/options for each branch." I am facing this problem and would appreciate some elaboration on specifically how I can use different schemes per branch. My solution can provision new tenants at runtime, so iterating through tenants in startup to add schemes is not a solution for me. Wondering if you could provide some guidance on what I could override to dynamically resolve schemes and options per request. PostConfigureCookieAuthenticationOptions, looks like a potential candidate that might allow me to do that, but not sure if that allows me to adjust the options per request. IAuthenticationSchemeProvider sounds like another potential thing to implement. Could you offer a suggestion of the right thing to implement/override to achieve a way to resolve auth scheme/options on a per request basis? Thanks in advance for any advice! |
Alternatively you could also try replacing the If you had a implementation that was branch aware, that should be a step in the right direction. You'll still have to figure out how to resolve the appropriate options, but you could do that via plugging in your own IOptionsMonitor that is branch aware. |
Thank you! Really appreciate the prompt response! I will give IAuthenticationHandlerProvider a try. Can you tell me what is the purpose of IOptionsMonitor? what is it used for? I'm familiar with IOptions of T but IOptionsMonitor is new to me. |
Its basically the same thing, it just adds the ability to name options |
Thanks! |
You can configure them however you like the underlying options haven't changed that much |
Awesome, thanks very much! Will let you know how it goes or if I run into any troubles, working on the netcore20 branch in my open source project here https://github.com/joeaudette/cloudscribe , just got it to build but mainly by commenting out the broken bits, now I will try to get it working again following your advice. |
I'm trying to bring our owin authentication code to aspnetcore 2.0, so much stuff have changed ! |
So I'm just trying to add a simple claim based on a boolean column in my I set up an I might not be approaching the problem correctly, but I'm not sure what the correct way to solve this problem is. All I want to do is check if a I kinda just want a simple function I'll put the problem clearly here: Thanks for any advice available. |
@HaoK thanks to your previous help I managed to get my multi-tenant cookie auth working again. Now I am moving on to working on getting my multi-tenant social auth working and could use more advice about what to implement in order to resolve correct MicrosoftAccountOptions per tenant. I am working against preview2 and I see there are additional changes after preview2 such as use of IOptionsSnapshot in preview2 vs IOptionsMonitor in the current code. Since I am targeting preview2 for the moment, I was able to handle the mutlit-tenant cookie auth by implementing a custom
Since that worked I figured a similar solution should work for Microsoft Account Authentication by implementing a custom
But currently I'm getting an error:
I had a similar null reference when I first implemented the cookie auth but was able to solve it by taking a dependency on
and calling PostConfigure on my options which seemed to solve whatever was null on my cookie options. But I don't see anything similar to that to call for my MicrosoftAccountOptions, there exists MicrosoftAccountConfigureOptions but it is internal so I cannot use it even if it would solve the problem. Wonder if you could shed any light or advice on how best to override the options per request based on my resolved tenant. Mainly I need to adjust the clientid, clientsecret, and callbackpath per tenant. |
@HaoK it looks like what is null on my MicorosoftAccountOptions is StateDataFormat What would be the correct way to populate that? |
@HaoK thank you! that got me past the null error, but now after redirecting back from microsoft I get
any ideas about that? |
wait it is saying invalid client, I thought I was using credentials that already worked, I will double check first that it works in my 1.1 branch as it did before |
@HaoK thank you again, that got it working! Somehow my client secret got corrupted in the db but once I fixed that I was able to get it working! Really appreciate all your help! |
I'm starting to get the new model 😄 , it handles challenges in a more clean way, but:
All in all, good job ! |
I have a question about IClaimsTransformation. |
Why can't you continue to use OnAuthorizationCodeRecieved? |
Hi @HaoK Still i do not think it's a very good design, those functions are called Events, and they look like side effect free functions, instead you can change "something" on the input parameters and have some side effect. I would call those Callbacks, and make the side effect explicit on the return type, so you are sure on what you can affect. |
That's exactly how good old events/event handlers work 😅 E.g https://msdn.microsoft.com/en-us/library/system.componentmodel.canceleventargs(v=vs.110).aspx |
@Banashek You are correct. Just add a new Claim. Look at an example here https://marcusclasson.com/2017/08/03/adding-authentication-in-aspnetcore-2-0/ At the bottom I add a custom claim (like user id) |
I recently went through the same issues you were having where I needed different authentication for each branch. We have a multi-tenant setup where each branch/tenant would have their own certificates etc for JwtBearer. I ended up with a different approach than was discussed here due to their comment "A workaround could be to use different schemes/options for each branch" and not seeing this thread until after completing the work. I thought I'd post here in case others are having issues as there isn't much guidance out there. I set up all my different authentications for each branch with a specific scheme name:
I then created a custom
This allowed me to keep everything else with the default implementation and have each individual scheme be unaware of any custom setup. I haven't fully vetted this as it's using Preview2 and is a POC but appears to be functioning properly. |
Having some issues updating from preview2 to rtm. Where can I find the code for the default implementation of
? |
nevermind, it is generic, I forgot https://github.com/aspnet/Options/blob/dev/src/Microsoft.Extensions.Options/OptionsMonitor.cs |
Having issues porting our app to .NET Core 2.0 Background: This is an API app with Swagger access. The app uses Azure AD as provider
The following works as above pre 2.0:
Tried the following after ported to 2.0 In ConfigureServices(...)
In Configure(...)
|
What behavior are you expecting? There's no more automatic* anything in 2.0, you can only have a single "default scheme". |
I'm hoping someone can give me some guidance with a question I posed here, https://stackoverflow.com/questions/47804064/what-is-the-equivalent-of-useoauthbearerauthentication-in-an-asp-net-core-2-appl. I have a resource server using ASP.NET Core 2 that needs to be configured to read an access token that's an encrypted AuthenticationTicket. Previously, I could configure bearer auth with Is there something more generic I can use? |
Maybe this is a dumb question but can I ask why? It seems this new design adds complexity while removing the ability to gracefully handle several use cases the previous design handled well. The only arguable improvement seems to be the introduction of the unified auth middleware. But providing an abstract auth middleware base class for implementors to inherit could have encapsulated that shared functionality without adding this extra complexity or dropping support for existing use cases. What am i missing? |
@bungeemonkee because maintaining the token server was costly and the old bearer middleware primarily interfaced with that token server. Also, JWTs and OpenIdConnect have gained broad adoption. See https://github.com/aspnet/Security/issues/1235 for tracking this request. |
In addition, there are several commercial and open source token servers that are feature rich and are being maintained. Here's one such open source token server that's even recommended by the ASP.NET team: http://identityserver.io/ |
@HaoK Maybe this isn't the right place to ask, but I keep seeing statements like this (in this thread and elsewhere):
What I can't figure out is why ASP.NET works so hard to avoid a principal with multiple identities -- that's the whole point of separating the principal from its identities. I'm new to Identity, but as I dig into it, I've begun to think that Identity persistence is completely broken -- claims are directly associated to the user (principal), rather than being grouped by identities (which should then be associated with the user / principal). I'm starting to get the impression that the same mistake extends further into ASP.NET than just Identity persistence. I recognize the typical use-case probably just needs one active identity, but that would work in a correct principal / identities / claims relationship, whereas the current single-identity assumption more or less rules out a multiple-identity scenario. |
Not sure what you are specifically talking about. When we say 'identity' we are typically referring to the ClaimsPrincipal for the user. Both the authentication stack and asp.net core identity are about producing claims principals as the core responsibility of the authentication system is having named 'authentication schemes' producing ClaimsPrincipals. For Identity, there's a IUserClaimsPrincipalFactory interface: https://github.com/aspnet/Identity/blob/dev/src/Microsoft.Extensions.Identity.Core/IUserClaimsPrincipalFactory.cs. The auth stack is a bit more involved, but you can dig into that in: https://github.com/aspnet/HttpAbstractions/blob/dev/src/Microsoft.AspNetCore.Authentication.Abstractions |
In the real world, everyone has lots of different identities:
If I build a site that has site-specific user information (maybe a local username, an account type, join-date, that kind of thing), that information is the site-specific identity for that principal -- the user. After the user also authorizes my site to post to Facebook on his behalf, it's still the same user / principal, he has just picked up an additional identity (Facebook auth):
But unless I'm overlooking something, ASP.NET Identity persistence doesn't seem to have the ability to store the details of a single principal (user) with multiple identities, each with their own claims. All that stuff exists in the Security assemblies ( I actually have to implement that exact scenario relatively soon. A site I'm working on will have a lot of site-specific user information, but the users will also be authorizing the site to do things on their behalf using a pretty large number of third-party sites. I'm pretty sure we're just going to roll our own via IdentityServer, but I'm still trying to understand the ASP.NET Identity model, I'm sure it will come up again elsewhere. |
I already gave you the link to the interface that's responsible for building the ClaimsPrincipal in identity, if your app wants to build multiple ClaimsIdentities in the principal, just plug in your own UserClaimsPrincipalFactory and you are in full control. |
I understand I can build my own. I was hoping to learn why the concept of multiple identities per principal is not supported by ASP.NET Identity's persistence model. |
Identity is mostly focused on saving user data, you can build whatever authentication data structure you want from that via the ClaimsPrincipalFactory. Identity is not designed around persisting ClaimsIdentity/Principals, its about storing user data, which is typically used along with other things to produce the ClaimsPrincipals used for authentication. |
@MV10 you know IdentityServer uses ASP.NET Core Identity to store user information? |
@davidfowl It can, yes ... but we're also avoiding EF dependencies, so neither out-of-the-box implementation was an option for us. @HaoK Thanks. Mostly I wanted to confirm I wasn't overlooking something. The docs about this stuff are kind of all over the place (probably another reason we're going with Identity Server, their start-from-empty Quickstart walk-throughs make it a breeze to understand all the moving parts). I appreciate the discussion. |
I'm following the example from JwtBearerHandler while using an internal token generator. I'm having problems with the OnAuthenticationFailed callback. Basically, without throwing an error in here, the pipeline is allowed to continue to execute the controller code despite an error http status code being set. If I do throw an error in that callback, I have two problems.. The first is that it gets wrapped into a 500 error (because anything done with the context, seems ignored and thus the default error handler gets called -- I also tried disabling developer exception page to no avail). Secondly, I can't use an errorFilter to try to handle the error (though I think this is intentional). I've tried using app.UseStatusCodePages and combinations thereof. I do not want to do a redirect to an error controller through the error handling middleware as we'd then lose the context of the request. Can someone give me a recommendation on how to fix these? Thanks! |
It seems like some of the problems I linked to above were fixed by adding a Context.ForbidAsync before AuthenticateResult.Fail(with the exception). However, it does not work in Swagger -- swagger just shows no response with no status code. Also, it executes twice for some reason -- one thing I noticed on the second execution, was that the the c.Response.HasStarted was true in the OnAuthenticatedFailed callback. The first time, it was false. Definitely seems like a problem. @HaoK can you tell me if any of these problems were fixed with netcoreapp2.1? |
@timdoke I agree, that flow control is busted. I've filed #1613. |
Great, thanks @Tratcher. I added an additional few things I was noticing on this stackoverflow post.. https://stackoverflow.com/questions/48327456/netcoreapp2-0-swagger-custom-authentication-failure-gives-no-result. Regarding your solution, I was under the impression that with authentication handlers, errorFilters and error middleware are ignored. Isn't the above just an error middleware (plus, i need to be able to get the correct http status code, so it can't be a catch all?)? I appreciate the response.. |
Error middleware should work fine so long as you put them before UseAuthentication. MVC error filters don't work because your error happens outside of MVC. 500 is the expected error code for internal exceptions. What else did you have in mind? |
So JwtBearer's OnAuthenticationFailed is a ResultContext https://github.com/aspnet/Security/blob/dev/src/Microsoft.AspNetCore.Authentication.JwtBearer/Events/AuthenticationFailedContext.cs which means it doesn't support control flow. In 2.0 Only context's that derive from Or support control flow. |
@Tratcher from the sample, it looked like I should be able to handle this authentication failure inside the OnAuthenticationFailed callback. It is also incredibly unclear how I'm supposed to know to put that middleware exception handler before UseAuthentication.. |
Which sample? The one in Security is out dated. |
@Tratcher Is there a new sample? I'm checking the one in Security.. |
No, part of #1613 is to update the sample. |
I was wondering if anyone out there has a working ASP.NET Core 2.0 Web API (e.g. the Todo sample app) sample app with AWS Cognito as the identity server? Not looking for a tutorial, just working app where just the config needs to be changed (e.g. options.Authority) Update: figured it out, quite simple in the end (as you'd expect), I'll post a sample / skeleton ASP.NET Core 2.0 Web API to github as soon as I get a chance (in the mean time happy to help anyone else out - just shout) |
We periodically close 'discussion' issues that have not been updated in a long period of time. We apologize if this causes any inconvenience. We ask that if you are still encountering an issue, please log a new issue with updated information and we will investigate. |
How does this work behind a reverse proxy and AddFacebook now? I have a scenario where my proxy does ssl termination and the app is running in a container on http. Previously, app.UseForwardedHeaders would resolve the issue, but it's called too late now so doesn't seem to work anymore. |
Called too late? Note we don't track closed issues. Please open a new issue with more detail about your Startup. |
Nvm, figured it out, useforwardedheaders was just more sensitive than I expected. I included an extra header that the proxy didn’t forward... |
Related discussion thread of issues/questions about aspnet/Announcements#262
The text was updated successfully, but these errors were encountered: