asp.net-mvc - 需要使用 Web 应用程序和 Web API 进行双重身份验证吗?

标签 asp.net-mvc asp.net-web-api

我被困在如何解决以下问题上。
我将首先描述我的应用程序在一般情况下的样子。

[ASP MVC(Angular 应用程序)]

  • 使用 Owin cookie

  • [ WEB API 2 ]
  • 使用 Oauth token 承载

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

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

    问题:现在我必须为每个对 的请求使用一个 token 。网络API 2 访问 Controller 中的不同方法。这意味着我必须再次登录用户,但这次是通过 AngularJs。使用/token 路由。

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

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

    问候!

    最佳答案

    我的 WebAPI 支持 token 和 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
        });
    }
    

    关于asp.net-mvc - 需要使用 Web 应用程序和 Web API 进行双重身份验证吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28286913/

    相关文章:

    c# - 从服务器删除上传的文件

    c# - 我的机器上无法识别 Web API

    asp.net-mvc - ASP.net MVC 3-在OnActionExecuting中获取发布的JSON数据

    c# - ObjectId 数组的 MongoDB C# BsonRepresentation

    javascript - Ajax 调用无法从 Web api 服务获取返回消息

    javascript - 在 asp.net web api 中添加一个额外的 get 方法

    c# - 处理更新详细记录的 RESTful 方式

    mysql - 打开连接时发生内部连接 fatal error

    c# - 尝试使用 HttpClient 上传文件时来自 Web Api 的 RequestEntityTooLarge 响应

    c# - 在另一个解决方案中找到的类库的 "Cannot find or open the PDB file"