aspnet / AspNetKatana

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

OpenIdConnectAuthenticationOptions causes System.Net.Http to not be found #425

Closed DataJuggler closed 3 years ago

DataJuggler commented 3 years ago

I fixed it thanks to this, the rebuilding binding redirects did it. https://stackoverflow.com/questions/38408167/could-not-load-file-or-assembly-system-net-http-version-4-0-0-0-culture-neutr

I have been on this for a week. I upgraded our .Net Framework 4.6.1 app to 4.8, reinstalled all the nuget packages, and everything compiles and runs, unless I add the OpenIdConnectAuthenticationOptions then I get an error.

Here is part of the code in Startup.cs.

// Configure the db context, user manager and role manager to use a single instance per request app.CreatePerOwinContext(WebDbContext.Create); app.CreatePerOwinContext(ApplicationUserManager.Create); app.CreatePerOwinContext(ApplicationSignInManager.Create);

        // Enable the application to use a cookie to store information for the signed in user
        // and to use a cookie to temporarily store information about a user logging in with a third party login provider
        // Configure the sign in cookie
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login"),
            Provider = new CookieAuthenticationProvider
            {
                // Enables the application to validate the security stamp when the user logs in.
                // This is a security feature which is used when you change a password or add an external login to your account.  
                OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, AppUser>(
                    validateInterval: TimeSpan.FromMinutes(30),
                    regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
            }
        });
        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

        // Enables the application to temporarily store user information when they are verifying the second factor in the two-factor authentication process.
        app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));

        // Enables the application to remember the second login verification factor such as phone or email.
        // Once you check this option, your second step of verification during the login process will be remembered on the device where you logged in from.
        // This is similar to the RememberMe option when you log in.
        app.UseTwoFactorRememberBrowserCookie(DefaultAuthenticationTypes.TwoFactorRememberBrowserCookie);

        app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

        app.UseCookieAuthentication(new CookieAuthenticationOptions());

OpenIdConnectAuthenticationOptions options = new OpenIdConnectAuthenticationOptions(); options.ClientId = clientId; options.Authority = authority; options.RedirectUri = redirectUri; options.PostLogoutRedirectUri = redirectUri; options.Scope = OpenIdConnectScope.OpenIdProfile; options.ResponseType = OpenIdConnectResponseType.CodeIdToken; options.TokenValidationParameters = new TokenValidationParameters() { ValidateIssuer = false }; options.ProtocolValidator = new OpenIdConnectProtocolValidator() { RequireNonce = false }; // OpenIdConnectAuthenticationNotifications configures OWIN to send notification of failed authentications to OnAuthenticationFailed method options.Notifications = new OpenIdConnectAuthenticationNotifications { AuthenticationFailed = OnAuthenticationFailed };

        // if I comment this out, our app runs (without MS Identity, but runs with our authentication using Owin)
       // if I uncomment it, I get a System.Http 4.0.0 could not be found.
        app.UseOpenIdConnectAuthentication(options);

I have tried upgrading to 4.3.4, tried the 4.0.0 version it looks for, tried redirecting all versions, and still get this error:

Is this a known issue, because I have searched and not found anything about conflicts with this.

[FileLoadException: Could not load file or assembly 'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)] Microsoft.Owin.Security.OpenIdConnect.OpenIdConnectAuthenticationMiddleware..ctor(OwinMiddleware next, IAppBuilder app, OpenIdConnectAuthenticationOptions options) +0 lambda_method(Closure , OwinMiddleware , IAppBuilder , OpenIdConnectAuthenticationOptions ) +55

[TargetInvocationException: Exception has been thrown by the target of an invocation.] System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) +0 System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) +168 System.Delegate.DynamicInvokeImpl(Object[] args) +124 Microsoft.Owin.Builder.AppBuilder.BuildInternal(Type signature) +223 Microsoft.Owin.Host.SystemWeb.OwinAppContext.Initialize(Action1 startup) +482 Microsoft.Owin.Host.SystemWeb.OwinBuilder.Build(Action1 startup) +40 Microsoft.Owin.Host.SystemWeb.OwinHttpModule.InitializeBlueprint() +70 System.Threading.LazyInitializer.EnsureInitializedCore(T& target, Boolean& initialized, Object& syncLock, Func`1 valueFactory) +119 Microsoft.Owin.Host.SystemWeb.OwinHttpModule.Init(HttpApplication context) +106 System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +523 System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +176 System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +220 System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +303

[HttpException (0x80004005): Exception has been thrown by the target of an invocation.] System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +659 System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +89 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +188

Tratcher commented 3 years ago

Can you share your packages.config? Something is referencing the System.Net.Http package when it shouldn't, that library is available in the framework.

Tratcher commented 3 years ago

Oh, looks like you've fixed it? I'll close this then.

DataJuggler commented 3 years ago

Yeah, I have other issues, but my issue I created there was fixed by updating binding redirects.

Thanks, sorry I didn’t mark it as closed.

From: Chris Ross @.> Sent: Friday, July 30, 2021 2:23 PM To: aspnet/AspNetKatana @.> Cc: Corby Nichols @.>; Author @.> Subject: Re: [aspnet/AspNetKatana] OpenIdConnectAuthenticationOptions causes System.Net.Http to not be found (#425)

Oh, looks like you've fixed it? I'll close this then.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/aspnet/AspNetKatana/issues/425#issuecomment-890104936, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AGI4P7O24R3NPO27LAKZF73T2L325ANCNFSM5BI7GJYQ.