当前位置: 首页 > 编程语言 > C#

使用AAD对Azure API App进行身份validation时出现401错误分享

时间:2023-04-10 12:21:33 C#

C#学习教程:使用AAD验证AzureAPI应用程序时出现401错误我使用2.8.1SDK重新部署了我的服务,我可以使用AAD或Microsoft帐户通过浏览器登录该服务,并使用Swagger测试该服务。但是,我试图让客户端使用ClientId和Secret访问服务。该代码能够从AAD获取访问令牌,但每当我尝试访问其中一个服务资源时,我都会收到401错误。当我调试该服务时,我在日志中看到以下内容:Microsoft.Azure.AppService.AuthenticationVerbose:0:Receivedrequest:GEThttps://[myService].azurewebsites.net/api/[myResource]Microsoft.Azure。AppService.Authentication警告:0:JWT验证失败:IDX10214:受众验证失败。受众:“https://[myService].azurewebsites.net/”。不匹配:validationParameters.ValidAudience:'[AADClientId]'或validationParameters.ValidAudiences:'http://[myService].azurewebsites.net'。Microsoft.Azure.AppService.Authentication信息:0:发送响应:401.71未经授权线程0x3b00已退出,代码为0(0x0)。似乎问题出在提供Therequestedaudienceishttps,但validParameters.ValidAudiences集合只包含http。我看不到任何配置受众的方法,当VisualStudio2015创建应用服务时,似乎正在设置基于http的受众。有没有办法手动编辑ValidAudience集合?作为参考,我的客户代码是:privatestaticvoidMain(string[]args){stringapp_id_url="https://[myService].azurewebsites.net/";字符串authority="https://login.windows.net/[myDirectory].onmicrosoft.com/";stringclientId="[AADClientId]";stringclientSecret="[AAD客户端密码]";stringapiBaseUrl="https://[myService].azurewebsites.net/";stringaadToken=GetTokenForApplication(authority,clientId,clientSecret,app_id_url);varapiClient=newHttpClient{BaseAddress=newUri(apiBaseUrl)};apiClient.DefaultRequestHeaders.Authorization=newAuthenticationHeaderValue("Bearer",aadToken);varapiResponse=apiClient.GetAsync(apiBaseUrl+@"api/[myResource]").Result;字符串apiResponseContent=apiResponse.Content.ReadAsStringAsync().Result;Console.WriteLine(apiResponseContent);}publicstaticstringGetTokenForApplication(stringauthority,stringclientId,stringclientSecret,stringresourceUrl){认证上下文authenticationContext=newAuthenticationContext(authority,false);ClientCredentialclientCred=newClientCredential(clientId,clientSecret);AuthenticationResultauthenticationResult=authenticationContext.AcquireToken(resourceUrl,clientCred);字符串令牌=authenticationResult;观众关于您可以有两个选择:选项1.尝试使用WebAPI客户端ID作为AcquireToken方法“资源”参数而不是其Uri来获取令牌。选项2.如果前面的方法不起作用,您必须使用AzureResourceExplorer修改AppServiceAPI的身份验证设置。导航到您的WebAPI,在配置节点下找到authSettingsJSON文档,然后修改(更改为读/写模式后)数组allowedAudiences以满足您的需要。在您的情况下,您可能需要将http更改为https在我的ASP.NET4.5Web应用程序中,我发现必须指定有效的受众以避免引发运行时异常。以上就是C#学习教程:使用AAD验证AzureAPIApp分享时出现401错误的全部内容。如果对大家有用,需要进一步了解C#学习教程,希望大家多多关注——privatestaticstring_tenant=ConfigurationManager.AppSettings["ida:Tenant"];私有静态字符串_realm=ConfigurationManager.AppSettings["ida:Wtrealm"];私有静态字符串_metadataAddress=string.Format("{0}/{1}/federationmetadata/2007-06/federationmetadata.xml",_aadInstance,_tenant);私有静态字符串_authority=String.Format(CultureInfo.InvariantCulture,_aadInstance,_tenant);publicvoidConfigureAuth(IAppBuilderapp){app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);app.UseCookieAuthentication(newCookieAuthenticationOptions());nValidationParameters=newTokenValidationParameters{ValidAudiences=newstring[]{“spn:”+_realm}}});}}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如有转载请注明出处: