我已经搜索了一段时间,发现一些有类似问题的帖子,要么没有答案,要么对我不起作用。
背景:我在web.config中有一个使用Windows身份验证的带有C#Web API 2后端的Vue.JS应用程序,因为这是我们必须在公司防火墙后设置站点的方法。我最初有一个可以使用的Bearer令牌,但是公司的合规团队不喜欢可以通过浏览器通过本地或会话存储访问令牌,因此我切换到cookie身份验证。我在Startup.cs中有OWIN Cookie身份验证设置:
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
CookieHttpOnly = true,
CookieSecure = CookieSecureOption.SameAsRequest,
ExpireTimeSpan = TimeSpan.FromMinutes(5),
LogoutPath = new PathString("/api/logout"),
Provider = new CookieAuthenticationProvider(),
SlidingExpiration = true
});
登录控制器使用以下代码:
IOwinContext context = HttpContext.Current.Request.GetOwinContext();
IAuthenticationManager authManager = context.Authentication;
UserPrincipal user = AdAuthenticationService.GeneratePrincipal(login["Username"], login["Password"]);
ClaimsIdentity identity = AdAuthenticationService.CreateIdentity(user);
authManager.SignIn(identity);
Cookie确实会生成,但是API调用上的[Authorize]属性似乎针对来自IIS的登录用户上下文(而不是Cookie)进行了验证,因此即使没有生成Cookie,API调用也可以成功。