4

我有一个 AngularJS + MVC + WebAPI,我正在尝试: - 使用标准(个人帐户)进行 MVC 身份验证;- 使用相同的用户和密码进行基于 WebAPI 的身份验证。

问题,从 AngularJS 一切正常,cookie 交换发生,Web API 返回值,但是当我试图从 Postman 访问 WebAPI 时,我得到一个重定向到登录页面而不是 401 Unauthorized。

实现这一目标的最简单方法是什么?我必须继承授权并手动实现逻辑吗?

谢谢

4

3 回答 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);
                    }
                }
            };
        });
于 2015-10-24T10:37:37.023 回答
3

您可以简单地为 Redirect 事件应用自定义操作。在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);
            }
        }
    });
}
于 2015-10-21T20:38:51.850 回答
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 属性标记。

来自AutomaticChallenge 的AuthenticationOptions.cs内联帮助:

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

于 2015-12-17T10:30:11.337 回答