Skip to content

WebApi Updated to latest Microsoft.Owin.Security.* packages and cannot find IIssuerSecurityTokenProvider inteface #28

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

Closed
vazans opened this issue Mar 19, 2018 · 25 comments

Comments

@vazans
Copy link

vazans commented Mar 19, 2018

Hi, I was running properly on protecting the WebApi with AD B2C AuthBearer token, after I upgraded to latest packages of Microsoft.Owin.Security.* I cannot find the definition for IIssuerSecurityTokenProvider anymore but when I try to use IIssuerSecurityKeyProvider instead of IIssuerSecurityTokenProvider I am running into a whole host of other issues.
Can somebody point me in the correct direction? specifically how Do I get the Owin setup part with code samples using the latest packages.

@vazans
Copy link
Author

vazans commented Mar 19, 2018

@parakhj @phatcher , I see both of you have a similar issue, Do you have any fix for this - Thanks

@phatcher
Copy link

@vazans The Azure B2C team are not very communicative - the roadmap is updated very infrequently and they don't keep us informed of what's happening at all.

They are also not very responsive to change in my opinion, e.g. we've been waiting for over 18 months for on-behalf of flows and we still have to use ADAL 2 to talk to Graph API which is well behind the current versioning.

I'd suggest reverting to the earlier libraries

@vazans
Copy link
Author

vazans commented Mar 20, 2018

Thanks @phatcher, I will revert back, we also updated the .net framework so might be a rats nest there trying to revert back just these packages.
@parakhj if you are listening Please help us out here, updating packages and no documentation on new packages make it really hard for me to trust this offering.
I will KEEP this one OPEN until get an actual resolution.

@parakhj
Copy link
Contributor

parakhj commented Mar 20, 2018

I had noticed @phatcher's comment and flagged it. I have been working to get some resources committed to improving our samples. If there is a list of issues that you would urgently want fixed, please let me know. I can get those prioritized first

@vazans
Copy link
Author

vazans commented Mar 21, 2018

@parakhj nice to see you addressing issues, what I need urgently should be very simple, In the samples give here what needs to change after all packages are moved latest and .net is 4.7

@parakhj Please answer these simple 2 steps and this will save a ton of time.
Environment: .net 4.7/Web API 2.1/All packages updated to latest stable versions.

  1. In file startup.auth.cs, how do we register an app for ADB2C Auth? What needs to change below?
    Note: IIssuerSecurityTokenProvider not available anymore
    public void ConfigureAuth(IAppBuilder app)
    {
    TokenValidationParameters tvps = new TokenValidationParameters
    {
    // Accept only those tokens where the audience of the token is equal to the client ID of this app
    ValidAudience = ClientId,
    AuthenticationType = Startup.DefaultPolicy
    };

         app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions
         {
             // This SecurityTokenProvider fetches the Azure AD B2C metadata & signing keys from the OpenIDConnect metadata endpoint
             AccessTokenFormat = new JwtFormat(tvps, new OpenIdConnectCachingSecurityTokenProvider(String.Format(AadInstance, Tenant, DefaultPolicy))) // This does not work
         });
     }
    
  2. in file OpenIdConnectCachingSecurityTokenProvider.cs , how do we implement the replacement for IIssuerSecurityTokenProvider or other options

Thanks and marking this an URGENT Request,

@vazans
Copy link
Author

vazans commented Mar 22, 2018

I have put a detailed request of what I need above,
Can any one you help? @saraford @gsacavdm @microsoftopensource @rari2012 @spottedmahn @danieldobalian @parakhj - Reg How to use protect an .net WebApi 2.1(.net framework > 4.7) with AD B2C, The code shown in sample is outdated and does not work with latest packages, Please help.
I have given full details as comment above.

@spottedmahn
Copy link
Contributor

spottedmahn commented Mar 22, 2018

Hi @vazans - I'm not sure exactly, sorry.

I do so this PR commit related to changing IIssuerSecurityKeyProvider.Tokens to IIssuerSecurityKeyProvider.SecurityKeys.

@spottedmahn
Copy link
Contributor

Hi @vazans - I was able to get the TaskService updated. It compiles now but I haven't tested it yet.

The source code is here.

Apparently the package reference to Microsoft.IdentityModel.Protocol.Extensions is old. Source.

@spottedmahn
Copy link
Contributor

Hi @vazans - I've updated the web app too. I've managed to test it and it appears to be working fine ⚡.

image

The branch with the changes is here.

@vazans
Copy link
Author

vazans commented Mar 26, 2018

Thanks @spottedmahn , will try this today and close when works.

@spottedmahn
Copy link
Contributor

Cool @vazans. FYI, I submitted a cleaner PR #29 than the branch reference above.

@phatcher
Copy link

phatcher commented Mar 27, 2018

@spottedmahn @vazans I've managed to get my code working but my code uses the BootstrapContext to push the JWT token down to the API.

I noticed that there's a breaking change in that the identity's BoostrapContext is no longer a BootstrapContext but just the JWT token itself - is this intended or a bug in the Microsoft.Identity.Client code?

@spottedmahn
Copy link
Contributor

Hey @phatcher - I'm not familiar w/ BootstrapContext. Seems like you doing this Access the JWT bearer token when using the JWT middleware in ASP.NET Core but in OWIN, correct?

@phatcher
Copy link

Once this change is committed I'll submit a PR to lift out some common code and improve the base ideas - I know this is a sample but it should be a bit cleaner.

@phatcher
Copy link

@spottedmahn Yes, that's correct, AFAIK that was the only way to do it in the earlier versions of the libraries

@phatcher
Copy link

Though technically I think it was the the WIF token that happens to be a JWT given the context i.e. you set TokenValidationParameters,SaveSigninToken = true and then it's available throughout the request, e.g.

public string AccessToken(ClaimsPrincipal principal = null)
{
    principal = principal ?? ClaimsPrincipal.Current;
    var cookieIdentity = principal.Identities.FirstOrDefault(x => x.AuthenticationType == "Cookies");

    if (cookieIdentity?.BootstrapContext is BootstrapContext context)
    {
        return context.Token;
    }

    return cookieIdentity?.BootstrapContext as string;
}

@spottedmahn
Copy link
Contributor

@phatcher gotcha. Maybe posting an issue here: AzureAD/azure-activedirectory-identitymodel-extensions-for-dotnet would be a good idea as they are the creators of TokenValidationParameters.

Or maybe it would be a function of OWIN so posting here: aspnet/AspNetKatana would be a good idea? I'm not sure.

@spottedmahn
Copy link
Contributor

Using idea 1 from here, you can add it as claim manually.

app.UseOpenIdConnectAuthentication(
                new OpenIdConnectAuthenticationOptions
                {
                    ...

                    // Specify the callbacks for each type of notifications
                    Notifications = new OpenIdConnectAuthenticationNotifications
                    {
                        ...
                        SecurityTokenValidated = OnSecurityTokenValidated
                    },
	     });

Add Claim on SecurityTokenValidated

image

Get IdToken Example

image

@phatcher

@phatcher
Copy link

I'm not sure that survives for the duration of the session or just for the request that authenticates it i.e. when I land on a new page in the same session TokenValidated won't be called again and I'm not sure whether user-assigned claims are persisted in the cookie; I went through something like this about 18 months ago when this first came out.

I've avoided scopes etc in the MVC app, apart from basic role checking, since I have to enforce it anyway in the API.

I'll try the first link - point is that it is a breaking change from the previous version, though I can see I will have fun when we go to .NET Core since IAuthenticationManager goes away so I'll have to revise the claims enrichment logic in the API

@spottedmahn
Copy link
Contributor

It is getting persisted between requests. OnSecurityTokenValidated is only called once as you suspected. It must be writing this claim to the cookie then.

https://localhost:44316/Home/Claims

image

Source Code

@phatcher
Copy link

The BootstrapContext has been changed to a string due to .NET Core not having BootstrapContext - see AzureAD/azure-activedirectory-identitymodel-extensions-for-dotnet#897

Would be nice to have some documentation/rationale as to why bother with the TokenCache etc in the MVC app rather than just pushing the JWT down to the API.

@phatcher
Copy link

@vazans @spottedmahn I remember why I didn't bother with tokens/scopes, the documentation https://docs.microsoft.com/en-us/azure/active-directory-b2c/active-directory-b2c-reference-oidc says that the only resource supported is the application itself...

"Currently, the only resource that you can request a token for is your app's own back-end web API. The convention for requesting a token to yourself is to use your app's client ID as the scope"

The example uses read and write scopes and validates them in the API, but this is a simple string match against the data in the scopes claim.

Couple of issues here....

  • For any reasonable app you might have hundreds of scopes e.g. task.read, task.create, task.update, task.delete. This makes it infeasible to request everything at login
  • Still can't return roles as claims from AAD B2C so we can't easily aggregate up to roles
  • If we can use scopes, how do we define the valid ones in the AAD B2C tenant?

@vazans
Copy link
Author

vazans commented Mar 29, 2018

@spottedmahn @phatcher, I moved to other pressing priorities here at work, but I have a working code that I think should 1) works on latest packages, .net 4.7 2) work without Owin 3) separate Authen/Authorization essentially [Autorize] would only validate Authentication. Authorization can be custom based on claims in the prinicipal.
Not Tested this yet, so give me a week and I will post the tested code.
@phatcher for your questions
•Still can't return roles as claims from AAD B2C so we can't easily aggregate up to roles

Again, we are not Authorizing so not an issue. I need to test this but I thought we get all roles in the token. if NOT might need to use graph API call to your user/tenant to get the scopes assigned.
•If we can use scopes, how do we define the valid ones in the AAD B2C tenant?
I have done this, will let you know in a week when I post my changes.

@parakhj
Copy link
Contributor

parakhj commented Apr 5, 2018

@spottedmahn helped us update the sample. Thanks @spottedmahn! Please check out the update and let me know if you find any other issues. Closing this one for the time being.

@parakhj parakhj closed this as completed Apr 5, 2018
@Paqi
Copy link

Paqi commented Sep 7, 2018

@parakhj Can you point me to documentation about all the breaking changes, and migration instructions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants