App*_*ere 65 asp.net-mvc oauth-2.0 asp.net-web-api owin
我有一个单页应用程序 - 或多或少基于MVC5 SPA模板 - 使用承载令牌进行身份验证.
该网站还有一些传统的MVC页面需要保护,但使用cookie身份验证.
在Startup.Auth中,我可以启用两种类型的授权:
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseOAuthBearerTokens(OAuthOptions);
Run Code Online (Sandbox Code Playgroud)
但是,这似乎有副作用,因为无论何时从SPA发送AJAX请求,它都会在标头和 cookie中发送承载令牌.
我真正想要的行为是,只有持有者令牌用于WebAPI调用,而只有用于MVC调用的cookie.
我还希望MVC调用在未经授权时重定向到登录页面(设置为CookieAuthenticationOption),但显然我不希望在进行API调用时发生这种情况.
有没有办法在一个应用程序中进行这种类型的混合模式身份验证?也许通过路径/路由过滤器?
App*_*ere 47
我想我是这样做的: -
Startup.Auth正在连接OWIN管道,因此在那里包含Cookies和令牌是正确的.但是对cookie选项的一个更改指定了它应该应用于的身份验证类型:
CookieOptions = new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie
};
Run Code Online (Sandbox Code Playgroud)
然后我需要将WebAPI配置为仅使用令牌:
public static void Configure(HttpConfiguration config)
{
// Configure Web API to use only bearer token authentication.
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
}
Run Code Online (Sandbox Code Playgroud)
这似乎实现了我想要的.WebAPI只使用承载令牌而不使用cookie,一些常规MVC页面在登录后使用cookie(使用AuthenticationManager).
| 归档时间: |
|
| 查看次数: |
30683 次 |
| 最近记录: |