我有一个独立的Blazor Webassembly项目,当我在Visual Studio中本地运行时,我正试图从中访问Azure Function应用程序,例如这样。
response = await Http.GetStringAsync("https://mytestsite897845.azurewebsites.net/api/Test");
我的Azure功能应用程序配置了Azure Active Directory身份验证/授权,我已设置"https://localhost:44351",并勾选"启用访问控制允许凭据"。
在"身份验证/授权"部分,如果我将"请求未通过身份验证时要采取的操作"设置为"允许匿名请求(无操作(",则上述调用工作正常,并且我会从"测试"Azure功能获得响应。
但是,一旦我将"请求未通过身份验证时要采取的操作"更改为"使用Azure Active Directory登录",则即使在我使用Active Directory B2C登录后,上述调用也会失败。我在浏览器中收到以下错误。
Access to fetch at 'https://login.windows.net/<my client id>/oauth2/authorize?response_type=code+id_token&redirect_uri=https%3A%2F%2Fmytestsite897845.azurewebsites.net%2F.auth%2Flogin%2Faad%2Fcallback&client_id=48fd8563-3826-4d79-b29a-907a17598afe&scope=openid+profile+email&response_mode=form_post&nonce=e428bf7812ef4327a710b1162353ac3d_20200820091540&state=redir%3D%252Fapi%252FTest'
(redirected from 'https://mytestsite897845.azurewebsites.net/api/Test') from origin 'https://localhost:44351' has been blocked
by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves
your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
GET https://login.windows.net/<my client id>/oauth2/authorize?response_type=code+id_token&redirect_uri=https%3A%2F%2Fmytestsite897845.azurewebsites.net%2F.auth%2Flogin%2Faad%2Fcallback&client_id=48fd8563-3826-4d79-b29a-907a17598afe&scope=openid+profile+email&response_mode=form_post&nonce=e428bf7812ef4327a710b1162353ac3d_20200820091540&state=redir%3D%252Fapi%252FTest
net::ERR_FAILED
crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: TypeError: Failed to fetch
System.Net.Http.HttpRequestException: TypeError: Failed to fetch
at System.Net.Http.BrowserHttpHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts, CancellationToken callerToken, Int64 timeoutTime)
at System.Net.Http.HttpClient.GetStringAsyncCore(Task`1 getTask, CancellationToken cancellationToken)
at BACClient.Pages.Counter.OnInitializedAsync() in .PagesCounter.razor:line 79
at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()
at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle)
有人知道我缺了什么吗?
你能分享一下你为配置Azure功能以针对Azure B2C(策略和应用程序(进行身份验证所采取的步骤吗?你提到的步骤与Azure AD B2C无关,而是与Azure AD有关。
您可能会发现此链接很有用,它解释了所需的实际设置:https://codemilltech.com/adding-azure-ad-b2c-authentication-to-azure-functions/#:~:text=%20Azure%20Function%20App%20can,以及%20thin%20Xamarin。