Joa*_*usa 4 asp.net-mvc asp.net-authentication asp.net-web-api asp.net-identity
我有一个AngularJS + MVC + WebAPI,我正在尝试: - 使用标准(个人帐户)进行MVC身份验证; - 使用相同的用户和密码进行基于WebAPI的身份验证.
问题,从AngularJS一切正常,cookie交换发生,Web API返回值,但是当我试图从Postman访问WebAPI时,我得到一个重定向到登录页面而不是401 Unauthorized.
实现这一目标的最简单方法是什么?我是否必须子类授权并手动实现逻辑?
谢谢
对于ASP.Net 5最新的beta8,答案是将以下内容添加到Startup.cs上的ConfigureServices:
services.Configure<IdentityOptions>(config =>
{
options.Cookies.ApplicationCookie.LoginPath = "/Account/Login";
options.Cookies.ApplicationCookie.CookieHttpOnly = true;
options.Cookies.ApplicationCookie.CookieSecure = CookieSecureOption.SameAsRequest;
options.Cookies.ApplicationCookie.Events = new CookieAuthenticationEvents()
{
OnRedirect = ctx =>
{
if (ctx.Request.Path.StartsWithSegments("/api") &&
ctx.Response.StatusCode == 200)
{
ctx.Response.StatusCode = 401;
return Task.FromResult<object>(null);
}
else
{
ctx.Response.Redirect(ctx.RedirectUri);
return Task.FromResult<object>(null);
}
}
};
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3012 次 |
| 最近记录: |