Blazor oauth2 被 cors 阻止 [英] Blazor oauth2 blocked by cors
本文介绍了Blazor oauth2 被 cors 阻止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在服务器端有一个控制器,如下所示:
I have a controller on the server side looking like this:
[HttpGet]
public IActionResult Test(string returnUrl = "/")
{
return Challenge(new AuthenticationProperties() { RedirectUri = returnUrl });
}
当我按下客户端的按钮时,我会调用:
And when I press a button on the client side i call:
await Http.GetAsync("/api/Login/Test");
问题是我在控制台中收到 cors 错误,但没有任何反应.如果我在浏览器(localhost/api/Login/test)中手动输入 url,它工作正常.
The problem is that I'm getting a cors error in the console and nothing happens. If i put in the url manually in the browser (localhost/api/Login/test) it works fine.
我添加了如下所示的 cors 策略:
I added a cors policy that looks like this:
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy",
builder => builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader());
});
和
app.UseCors("CorsPolicy");
这没有什么区别.
推荐答案
用
@inject Microsoft.AspNetCore.Components.Services.IUriHelper UriHelper
和
UriHelper.NavigateTo("/api/Login/Test", forceLoad: true);
这篇关于Blazor oauth2 被 cors 阻止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文