简体   繁体   English

集成 Microsoft Identity MSAL 后 azurewebsites.net webapp 上出现未经授权的 401 错误。 本地主机工作

[英]Unauthorised 401 error on azurewebsites.net webapp after integrating Microsoft Identity MSAL. Localhost works

My c# webapp on localhost is functional.我在 localhost 上的 c# webapp 可以正常工作。

However, after publishing to Azure, the website does not display any content and is returned with a 401 unauthorised error.但是,发布到 Azure 后,网站没有显示任何内容,并返回 401 未经授权的错误。

Reviewing the app's, application event log in azure portal, I see the following error.在 azure 门户中查看应用程序的应用程序事件日志,我看到以下错误。

Category: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware
EventId: 1
RequestId: 80001a7a-0002-f800-b63f-84710c7967bb
RequestPath: /
SpanId: |51dbc300-47e836e38a7ad2a4.
TraceId: 51dbc300-47e836e38a7ad2a4
ParentId: 

An unhandled exception has occurred while executing the request.

Exception: 
System.InvalidOperationException: Unable to resolve service for type 'System.Net.Http.IHttpClientFactory' while attempting to activate 'Microsoft.Identity.Web.AppServicesAuthenticationTokenAcquisition'.
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(Type serviceType, Type implementationType, CallSiteChain callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(ResultCache lifetime, Type serviceType, Type implementationType, CallSiteChain callSiteChain)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceDescriptor descriptor, Type serviceType, CallSiteChain callSiteChain, Int32 slot)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(Type serviceType, CallSiteChain callSiteChain)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateCallSite(Type serviceType, CallSiteChain callSiteChain)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.<>c__DisplayClass7_0.<GetCallSite>b__0(Type type)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.GetCallSite(Type serviceType, CallSiteChain callSiteChain)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.CreateServiceAccessor(Type serviceType)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired)
   at lambda_method(Closure , IServiceProvider , Object[] )
   at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.DefaultPageModelActivatorProvider.<>c__DisplayClass1_0.<CreateActivator>b__0(PageContext context)
   at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.DefaultPageModelFactoryProvider.<>c__DisplayClass3_0.<CreateModelFactory>b__0(PageContext pageContext)
   at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.CreateInstance()
   at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.InvokeInnerFilterAsync()
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Localization.RequestLocalizationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)

The following refers to the AddAuthetication services in startup.cs以下指的是startup.cs中的AddAuthetication服务

    services
       // Use OpenId authentication
       .AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
       // <AddSignInSnippet>
       // Specify this is a web app and needs auth code flow
       .AddMicrosoftIdentityWebApp(options =>
       {
           Configuration.Bind("AzureAd", options);
           options.Prompt = "select_account";
           options.Events.OnTokenValidated = async context =>
           { ....

I'm not sure how to resolve this error given the two different outcomes for azure hosted and localhost.鉴于 azure 托管和 localhost 的两种不同结果,我不确定如何解决此错误。

Your code does not show IHttpClientFactory DI and constructor, but most probably, this is happening because of the following reasons:您的代码没有显示IHttpClientFactory DI 和构造函数,但很可能是由于以下原因而发生的:

  1. IHttpClientFactory is trying to call AppServicesAuthenticationTokenAcquisition in the constructor and somehow it is not getting activated and then the Dependency Injection(DI) of IHttpClientFactory fails. IHttpClientFactory试图在构造函数中调用AppServicesAuthenticationTokenAcquisition并且不知何故它没有被activated ,然后IHttpClientFactory的依赖注入(DI)失败。
  2. And since authentication is not properly initialized, your app is still making calls to an authenticated API/service which is throwing 401 error.并且由于身份验证未正确初始化,您的应用程序仍在调用经过身份验证的 API/服务,这会引发401错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 跨默认域(* .azurewebsites.net)上的Azure Web应用程序的ASP.NET Core共享身份Cookie - ASP.NET Core Sharing Identity Cookie across azure web apps on default domain (*.azurewebsites.net) net.core和angular4应用的Cors问题-在azurewebsites.net域上无法正常工作 - Cors issue with net.core and angular4 app - works on azurewebsites.net domain not on custom 具有azurewebsites.net域的Identityserver4-如何获取SSL指纹 - Identityserver4 with azurewebsites.net domain - how to get ssl thumbprint 3次登录尝试后出现401未经授权的访问错误,如何重新加载页面? 在asp.net - How to reload page after 3 login attempts with 401 unauthorised access error? in asp.net 收到401未经授权的错误 - 401 Unauthorised Error Received https:// 是什么意思<myapp> .azurewebsites.net/.auth/login/aad/callback 是什么意思? - What does https://<myapp>.azurewebsites.net/.auth/login/aad/callback mean? 从 Azure 网站 (xyz.com) 检索默认站点 URL (azurewebsites.net) - retrieve the default site URL(azurewebsites.net) from an Azure website (xyz.com) 防止 AddRedirectToWwwPermanent() 在 ASP.NET Core 2.1 中将“www”添加到 *.azurewebsites.net 的前面 - Prevent AddRedirectToWwwPermanent() from adding "www" to the front of *.azurewebsites.net in ASP.NET Core 2.1 身份服务器 4 - API 在部署到服务器时返回 401 Unauthorized - Identity Server 4 - API returns 401 Unauthorised when deployed to servers 在PayPal Vault付款中获得(401)未经授权,在沙箱中可以正常工作 - Getting (401) Unauthorised in PayPal Vault payment, works fine in sandbox
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM