aspnet / AspNetKatana

Microsoft's OWIN implementation, the Katana project
Apache License 2.0
968 stars 334 forks source link

Could not load type 'System.IdentityModel.Tokens.TokenValidationParameters' #76

Closed ArtemAvramenko closed 7 years ago

ArtemAvramenko commented 7 years ago

I upgraded nuget packages:

and got an error:

Could not load type 'System.IdentityModel.Tokens.TokenValidationParameters' from assembly 'System.IdentityModel.Tokens.Jwt, Version=5.1.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.

   at Microsoft.Owin.Security.OpenIdConnect.OpenIdConnectAuthenticationOptions..ctor(String authenticationType)
   at Microsoft.Owin.Security.OpenIdConnect.OpenIdConnectAuthenticationOptions..ctor()

Is OWIN compatible with latest IdentityModel?

Tratcher commented 6 years ago

Ah, TVP moved namespaces: https://github.com/AzureAD/azure-activedirectory-identitymodel-extensions-for-dotnet/blob/rel/5.2.0/src/Microsoft.IdentityModel.Tokens/TokenValidationParameters.cs

IIssuerSecurityTokenProvider is now IIssuerSecurityKeyProvider. https://github.com/aspnet/AspNetKatana/blob/5d6e25b303e0efa27fde385750b59ff58ce194b5/src/Microsoft.Owin.Security.Jwt/IIssuerSecurityKeyProvider.cs

mvpkenlin commented 6 years ago

Oh....thanks.

May I know why removing IIssuerSecurityTokenProvider? Securities issue?

Any code sample on how to update IIssuerSecurityTokenProvider to being using IIssuerSecurityKeyProvider?

figuerres commented 6 years ago

@Tratcher thanks, now i know what to look for in my project to see if i can update to match.... soon i need to see if i can swap my identity server V3 to V4 and still keep the rest of my stuff working.

the one sucky thing about having packages is when the dependency for an update starts pulling in stuff and you can't see where it broke, we do not get a dialog that tells us that some package got updated and it has a breaking change off in some corner. when it's 2 or 3 packages not hard to find but when we have 30 or 40 or more ..... yuck!

Tratcher commented 6 years ago

@mvpkenlin here's how we updated it: https://github.com/aspnet/AspNetKatana/commit/5d58dab237663b694515a3adbb36dd246a7d4ca2#diff-02dcc6bdd8fadc8c97e5234d641548fc

GauravDhavale commented 6 years ago

Hi @Tratcher : I was also facing the TVP problem. So, I updated the related packages to the preview versions as suggested in this issue and also updated TVP namespace to Microsoft.IdentityModel.Tokens from System.IdentityModel.Tokens. However, I'm facing issues while fetching the access token using the ClaimsPrincipal and BootstrapContext as it's in the namespace System.IdentityModel.Tokens.

I used the below code in Startup.Auth.cs file

public void ConfigureAuth(IAppBuilder app)
        {
            app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

            app.UseCookieAuthentication(new CookieAuthenticationOptions());

            app.UseOpenIdConnectAuthentication(
                new OpenIdConnectAuthenticationOptions
                {

                    ClientId = clientId,
                    Authority = authority,
                    PostLogoutRedirectUri = postLogoutRedirectUri,
                    RedirectUri = postLogoutRedirectUri,
                    TokenValidationParameters = new TokenValidationParameters
                    {
                        SaveSigninToken = true
                    },
                    Notifications = new OpenIdConnectAuthenticationNotifications
                    {
                        AuthorizationCodeReceived = async context =>
                        {
                            var code = context.Code;
                            ClientCredential credential = new ClientCredential(clientId, appKey);
                            string signedInUserID = context.AuthenticationTicket.Identity.FindFirst(ClaimTypes.NameIdentifier).Value;
                            AuthenticationContext authContext = new AuthenticationContext(authority);
                            AuthenticationResult ar = await authContext.AcquireTokenByAuthorizationCodeAsync(code, new Uri(RedirectUri), credential, "https://graph.windows.net");
                        },
                        AuthenticationFailed = context =>
                        {
                            context.HandleResponse();
                            context.Response.Redirect("/Error?message=" + context.Exception.Message);
                            return Task.FromResult(0);
                        }
                    }
                });

        }

In another file in Home controller, I want to get the user information along with Access Token using below code:

  private Task GetTokenViaBootStrap()
        {
            return Task.Run(async () =>
            {
                var bc = ClaimsPrincipal.Current.Identities.First().BootstrapContext as BootstrapContext;

                string userName = ClaimsPrincipal.Current.FindFirst(ClaimTypes.Upn) != null ? ClaimsPrincipal.Current.FindFirst(ClaimTypes.Upn).Value : ClaimsPrincipal.Current.FindFirst(ClaimTypes.Email).Value;
                string userAccessToken = bc.Token;
                UserAssertion userAssertion = new UserAssertion(bc.Token, "urn:ietf:params:oauth:grant-type:jwt-bearer", userName);
                string userObjectID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
                string signedInUserID = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value;
                AuthenticationContext authContext = new AuthenticationContext(authority);
                ClientCredential cred = new ClientCredential(clientId, appKey);
                result = await authContext.AcquireTokenAsync("https://graph.microsoft.com", cred, userAssertion);
                UserAccessToken = result.AccessToken;
            });

        }

In the above code, I'm getting a NULL value for bc. Any help to resolve this issue?

togakangaroo commented 5 years ago

So this is still causing issues. I'm trying to run versions that have source symbols available but doing so causes this error. I can upgrade to 4.whatever but I haven't the foggiest of clues where to find the symbols nor source for it.

Tratcher commented 5 years ago

Comments on closed issues are not tracked, please open a new issue with the details for your scenario.

Note symbols nor sources are of much use for assembly load errors. Your packages.config is the primary place to check for mismatches. You want Microsoft.Owin 4.x and IdentityModel 5.x.