需要使用 Web 应用程序和 Web API 进行双重身份验证?

Double authentication with Web app & Web API needed?

我一直在思考如何解决以下问题。 我将从描述我的应用程序在一般情况下的外观开始。

[ASP MVC(Angular 应用程序)]

[ 网页 API 2 ]

这种情况正在发生: 用户访问应用程序并使用位于 ASP MVC 应用程序 中的登录表单进行身份验证并生成一个 cookie。

现在我决定使用 AngularJs 添加一些功能,这让我使用了 $resources 和 Web API 2。但是,这些功能只有在用户获得授权后才可用。

问题:现在我必须为每个对 Web Api 2 的请求使用令牌,以访问控制器内的不同方法。这意味着我必须再次登录用户,但这次是通过 AngularJs。使用 /token 路由。

我该怎么做? 我应该获取 cookie,检查其中的凭据并将其作为身份验证请求发送吗? 我可以在 Asp MVC 应用程序 中以相同的方法在表单身份验证中做一些事情吗?

请帮助我,这给了我很多开销。在 30 分钟内从一个简单的应用程序走到这个。我什至无法理解身份验证中的所有内容。

此致!

我的 WebAPI 支持令牌和 cookie 身份验证。

在启动过程中,我像这样注册身份验证:

private void ConfigureAuth(IAppBuilder app)
{
    //Token 
    app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions
    {
    });

    // Enable the application to use a cookie to store information for the signed in user
    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
        LoginPath = new PathString("/Account/Login"), 
        Provider = new CookieAuthenticationProvider
        {
            OnApplyRedirect = ctx =>
            {
                // this is to ensure that a 401 response is sent if the
                // user is not authenticated, rather than redirecting to
                // a logon page.
            }
        },
        CookieDomain = ".example.com" //might not need to set this
    });
}