ASP.Net MVC 6 + WebAPI身份验证 - 重定向MVC到登录但对WebAPI返回401错误

4
我有一个AngularJS + MVC + WebAPI的项目,其中我正在尝试: - 使用标准(个人账户)进行MVC身份验证; - 使用相同的用户和密码进行基于WebAPI的身份验证。
问题在于,从AngularJS中一切工作正常,cookie交换发生了,Web API返回了值,但是当我尝试从Postman访问WebAPI时,我却被重定向到登录页面,而不是401未经授权。
最简单的方法是什么?我需要子类化Authorize并手动实现逻辑吗?
谢谢
3个回答

7
对于最新的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);
                    }
                }
            };
        });

1
我认为在options.Cookies处应该改为config.Cookies,或者更可能的是将config更改为options - Brian Riley

3
你可以为重定向事件应用自定义操作。在 App_Start/Startup.Auth.cs 文件中找到 app.UseCookieAuthentication() 方法,然后像这样修改:
public void ConfigureAuth(IAppBuilder app)
{
    // some omitted configurations 

    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        // some omitted configurations 

        Provider = new CookieAuthenticationProvider
        {
            // some omitted configurations 

            OnApplyRedirect = context => 
            {
                // assuming your API's url starts with /api
                if(!context.Request.Path.StartsWithSegments(new PathString("/api")))
                    context.Response.Redirect(context.RedirectUri);
            }
        }
    });
}

抱歉,我只在标题中提到了请求是针对MVC6的,而没有在问题正文中提到。 - Joao Sousa

2
在RC1-Final(VS2015.1)中,我已经完成了以下操作: 在身份验证配置中将AutomaticChallenge设置为false,并将ApplicationCookieAuthenticationScheme设置为“ApplicationCookie”:
services.AddIdentity<AppUser>(options =>
        {
            // cut

            options.Cookies.ApplicationCookie.AutomaticAuthenticate = true;
            options.Cookies.ApplicationCookie.AutomaticChallenge = false;
            options.Cookies.ApplicationCookieAuthenticationScheme = "ApplicationCookie";
        })
            .AddUserStore<AppUserStore<AppUser, AppDbContext>>()
            .AddDefaultTokenProviders();

然后,对于我想要重定向到登录页面的控制器,我添加了ActiveAuthenticationSchemes =“ApplicationCookie”。
[Authorize(ActiveAuthenticationSchemes = "ApplicationCookie")]
    public async Task<IActionResult> Logout()
    {
        // cut
    }

但是其他控制器(在我的情况下是 WebAPI)我用没有参数的 Authorize 属性标记了它们。
AuthenticationOptions.cs 的内联帮助中获取 AutomaticChallenge 的说明:

如果为 false,则身份验证中间件只会在 AuthenticationScheme 明确指示时更改响应。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接