[英]CORS issue with oauth if redirected to Account/Login from [Authorized]
public class AccountController : Controller
{
public IActionResult Login(string returnUrl)
{
return new ChallengeResult(
GoogleDefaults.AuthenticationScheme,
new AuthenticationProperties
{
RedirectUri = Url.Action(nameof(LoginCallback), new { returnUrl })
});
}
...
我有上面显示的 controller。 如果我在浏览器地址栏中键入https://localhost:44378/Account/Login
它工作正常 - 我被重定向到 Google 授权页面,但如果我通过点击[Authorize]
或另一个类似属性重定向到此端点端点我收到如下错误:
从源“http”访问“https://accounts.google.com/o/oauth2/v2/auth?...”处的 XMLHttpRequest(重定向自“https://localhost:44378/api/some-action”) ://localhost:3000' 已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”header。
JIC,我在应用注册 Google 页面http://localhost:3000
添加到授权 JavaScript 来源和授权重定向 URI中。
为什么会这样以及如何解决?
编辑2:
我在同一个端口上创建了前端和后端的测试项目,问题仍然存在
(从“https://localhost:44336/weatherforecast”重定向)来自“https://localhost:44336”的来源已被 CORS 政策阻止
本地 HTTP 服务器(服务于https://localhost:[Port]/...
的服务器需要配置为响应 HTTP OPTIONS 请求,其中包含Access-Control-Allow-Origin: '*'
在跨域请求的情况下,浏览器(尤其是 chrome)在 HTTP GET 或 HTTP POST 之前对同一端点发出 HTTP OPTIONS 请求
在您的 Startup.cs 文件中尝试此操作。
这个在 class -> 只读字符串 MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
这在 ConfigureServices 方法中
services.AddCors(options =>
{
options.AddPolicy(name: MyAllowSpecificOrigins,
builder =>
{
builder.WithOrigins("http://localhost:4200");
});
});
用你的改变地址和端口
您也可以查看此链接: https://docs.microsoft.com/en-us/aspnet/core/security/cors?view=aspnetcore-5.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.