Asp.net 为什么context.Request.context.Authentication.sign不生成cookie?

Asp.net 为什么context.Request.context.Authentication.sign不生成cookie?,asp.net,cookies,asp.net-web-api,oauth,owin,Asp.net,Cookies,Asp.net Web Api,Oauth,Owin,我们有一个MVC5应用程序,我们在其中添加了Web Api控制器,以提供REST Api功能。我们已经使用自定义OAuth提供程序类通过OWIN管道成功地实现了OAuth身份验证 现在我们还想实现身份验证cookies,以保护服务器上的静态资源。我相信还有很多其他方法可以做到这一点,但是对资源的请求是直接指向该资源的链接,因此我不能使用我的OAuth令牌或任何其他机制,这就是为什么我们要使用cookie…浏览器已经发送了cookie,不需要任何更改 从我读到的所有内容来看,可以使用OWIN管道进

我们有一个MVC5应用程序,我们在其中添加了Web Api控制器,以提供REST Api功能。我们已经使用自定义OAuth提供程序类通过OWIN管道成功地实现了OAuth身份验证

现在我们还想实现身份验证cookies,以保护服务器上的静态资源。我相信还有很多其他方法可以做到这一点,但是对资源的请求是直接指向该资源的链接,因此我不能使用我的OAuth令牌或任何其他机制,这就是为什么我们要使用cookie…浏览器已经发送了cookie,不需要任何更改

从我读到的所有内容来看,可以使用OWIN管道进行承载令牌身份验证和Cookie身份验证。基本上,Web API将使用承载令牌,因为这是客户端提供的所有令牌,服务器上的特定静态资源请求将使用cookie,cookie在所有请求上发送

我们的问题是,下面的代码永远不会生成身份验证cookie。在整个管道中,我从未在响应上看到set cookie标题,这就是为什么我在管道中添加了Kentor cookie Saver…它应该会有所帮助

WebApiConfig.cs

...
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
...
...
app.UseOAuthBearerTokens(OAuthOptions);
// I was told this might help with my cookie problem...something to do with System.Web stripping Set-Cookie headers
app.UseKentorOwinCookieSaver();
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
  AuthenticationType = Microsoft.Owin.Security.Cookies.CookieAuthenticationDefaults.AuthenticationType,
  AuthenticationMode = Microsoft.Owin.Security.AuthenticationMode.Active,
  ExpireTimeSpan = TimeSpan.FromHours(4)
});
...
Startup.Auth.cs

...
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
...
...
app.UseOAuthBearerTokens(OAuthOptions);
// I was told this might help with my cookie problem...something to do with System.Web stripping Set-Cookie headers
app.UseKentorOwinCookieSaver();
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
  AuthenticationType = Microsoft.Owin.Security.Cookies.CookieAuthenticationDefaults.AuthenticationType,
  AuthenticationMode = Microsoft.Owin.Security.AuthenticationMode.Active,
  ExpireTimeSpan = TimeSpan.FromHours(4)
});
...
自定义OAuth提供程序

...
// Creates our claims and properties...keep in mind that token based authentication is working
CreatePropertiesAndClaims(acct, out properties, out claims);
if (IsAccountAuthorized(claims))
{
  AuthenticationProperties authProps = new AuthenticationProperties(properties);
  ClaimsIdentity claimsIdentity = new ClaimsIdentity(context.Options.AuthenticationType);
  claimsIdentity.AddClaims(claims);

  AuthenticationTicket ticket = new AuthenticationTicket(claimsIdentity, authProps);
  context.Validated(ticket);

  ClaimsIdentity cookieIdentity = new ClaimsIdentity(claims, Microsoft.Owin.Security.Cookies.CookieAuthenticationDefaults.AuthenticationType);
  context.Request.Context.Authentication.SignIn(cookieIdentity);    // This should create the auth cookie!??!
}
else
{
  context.SetError("Unauthorized", "You don't currently have authorization. Please contact support.");
}
...
请记住,基于令牌的身份验证正在工作,因此我假设是配置设置丢失或配置错误,或者是管道排序问题


谢谢大家!

我知道这是一个迟来的答案,但我们遇到了完全相同的问题。我的同事和我花了4个小时试图找出原因。这里有一个答案,希望能拯救其他人,避免他们的头撞在墙上

检查响应,您可以看到有
设置Cookie:.AspNet.Cookies=LqP1uH-3UZE-ySj4aUAyGa8gt….
但未保存Cookies。您需要做的是,在ajax调用中包括凭证

$.ajax({
    url: url,
    type: 'POST',
    xhrFields: {
        withCredentials: true
    }
)
我们正在使用Fetch API,它如下所示

fetch(url, {  
  method: 'post',  
  headers: {  
    "Content-type": "application/x-www-form-urlencoded; charset=UTF-8"  
  }, 
  credentials: 'include'  
})