【发布时间】:2014-10-03 20:45:25
【问题描述】:
我已将 Owin 配置为在身份验证时发出令牌和 cookie:
public void Configuration(IAppBuilder app)
{
var cookieOptions = new CookieAuthenticationOptions
{
AuthenticationMode = AuthenticationMode.Active,
CookieHttpOnly = true, // JavaScript should use the Bearer
//AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
CookieName = "MyCookie",
LoginPath = new PathString("/app/index.html#/login"),
};
var oAuthServerOptions = new OAuthAuthorizationServerOptions
{
AllowInsecureHttp = true,
TokenEndpointPath = new PathString("/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
Provider = new MyAuthorizationServerProvider(),
};
var oAuthBearerOptions = new OAuthBearerAuthenticationOptions
{
};
// Must be registered in this order!
app.UseCookieAuthentication(cookieOptions);
app.UseOAuthAuthorizationServer(oAuthServerOptions);
app.UseOAuthBearerAuthentication(oAuthBearerOptions);
}
这很好 - 它为我的 SPA 发出承载令牌以调用我的 API 和 cookie,因此我的旧学校 MVC 页面也可以登录。
但如果我在声明要使用 CookieAuth 之前注册 OAuth 服务器,则不会发出任何 cookie。换句话说,如果我这样做,它就不起作用:
app.UseOAuthAuthorizationServer(oAuthServerOptions);
app.UseCookieAuthentication(cookieOptions);
app.UseOAuthBearerAuthentication(oAuthBearerOptions);
另外,如果我取消注释这一行,它也不会发出 cookie:
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
我的问题是为什么在 Owin 的注册顺序很重要?还有为什么将cookie中的AuthenticationType设置为"ApplicationCookie"也会导致失败呢?
【问题讨论】:
标签: asp.net-mvc owin