有一个包含UI和API的解决方案。
UI
是一个ASP.NET Core MVC
项目,它只包含一个HomeController
和Index
视图,该视图加载在VueJS
应用程序中。
API
是一个ASP.NET Core Web API
,用于使用VueJS身份验证来输入VueJS应用程序。
部署
我们有一个原点( https://example.com ),其中API由https://exampel.com/api访问。
我们有两个起源( https://app.example.com )的UI
和( https://api.example.com )的API
。
对于新的设置,我们需要实现CORS (交叉起源),以允许其他网站使用API。因此,我们需要为OPTIONS
预飞请求启用匿名身份验证。
有什么办法可以防止这种情况发生吗?
发布于 2019-11-13 10:55:13
设法使用用于身份验证的自定义中间件解决该问题:
public async Task InvokeAsync(HttpContext context)
{
// Check if the request is a PREFLIGHT => OPTIONS request
if (context.User.Identity.IsAuthenticated || context.Request.Method == "OPTIONS")
{
await _next(context);
return;
}
await context.ChallengeAsync("Windows");
}
在启动过程中,添加管道的开头:
app.UseMiddleware<AuthenticationMiddleware>();
https://stackoverflow.com/questions/58832574
复制相似问题