【问题标题】:OWIN Cookie authentication in Self-Hosted Web Api自托管 Web Api 中的 OWIN Cookie 身份验证
【发布时间】:2015-08-05 14:59:28
【问题描述】:

我有一个 OWIN 自托管 Web Api 和一些 MVC Web 应用程序都在同一个域中。 Web 应用程序在服务器端调用 Web Api。他们像这样使用 OWIN cookie 身份验证:

public void ConfigureAuth(IAppBuilder app)
{
    app.CreatePerOwinContext<MyUserManager>(MyUserManager.Create);
    app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login"),
            Provider = new CookieAuthenticationProvider
            {
                OnValidateIdentity = 
                    SecurityStampValidator.OnValidateIdentity<MyUserManager, MyUser, Guid>(
                        validateInterval: TimeSpan.FromMinutes(30),
                        regenerateIdentityCallback: (manager, user) =>
                            user.GenerateUserIdentityAsync(manager),
                        getUserIdCallback: (id) => (new Guid(id.GetUserId())))
            }
        });
}

在同一个域中,当用户登录一个 web 应用程序时,cookie 在其他 web 应用程序中可用并且用户已登录。

我在我的自托管 web api 中实现 cookie 身份验证,如下所示:

public void Configuration(IAppBuilder appBuilder)
    {
        // Configure Web API for self-host. 
        HttpConfiguration config = new HttpConfiguration();

        config.MessageHandlers.AddRange(new List<DelegatingHandler>
        {
            new ServerContextInitializerHandler(), new LogRequestAndResponseHandler(), 
        });
        config.MessageHandlers.AddRange(ServiceLocator.Current.GetAllInstances<DelegatingHandler>());

        config.Services.Add(typeof(IExceptionLogger), new GlobalExceptionLogger());
        config.Services.Replace(typeof(IExceptionHandler), new GlobalExceptionHandler());

        config.Routes.MapHttpRoute( 
            name: "DefaultApi", 
            routeTemplate: "api/{controller}/{id}", 
            defaults: new { id = RouteParameter.Optional } 
        ); 

        appBuilder.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = "ApplicationCookie",
            Provider = new CookieAuthenticationProvider(),
            AuthenticationMode = Microsoft.Owin.Security.AuthenticationMode.Active
        });

        appBuilder.UseWebApi(config);
    }

我希望用户在 web api 中,因为它在同一个域中,并且 cookie 在收到的请求中都可用。 问题是 Request.GetRequestContext().Principal 为空(以及其他替代方案,如 Request.GetOwinContext().Authentication.User)。

我强调 Request.GetOwinContext().Request.Cookies 包含 Web 应用程序中可用的所有 cookie。

【问题讨论】:

    标签: authentication cookies asp.net-web-api owin self-hosting


    【解决方案1】:

    问这个问题已经过去了很长时间,但还没有答案,但我找到了原因,我只是分享一下。希望有用。 正如我所提到的,我在我的自托管 web api 中实现了 cookie 身份验证,并且身份验证 cookie 在 web api 中接收到的请求中可用。 问题是,在 Web 应用程序(用户登录的地方)中,OWIN 中间件将 ClaimsIdentity 数据编码为访问令牌并将其放入身份验证 cookie,并访问此 cookie 中的身份验证数据,对其内容进行解码。 Web 应用程序上的这种编码和解码是通过使用运行服务器的机器的机器密钥进行加密和解密的,并在自托管 Web api 中使用另一种称为 DPAPI 的方式完成。 所以,我在 web api 请求上有了身份验证 cookie,但是因为 OWIN 中间件试图使用 DPAPI 进行解密,它无法从 cookie 中提取访问令牌。

    【讨论】:

    • 谢谢!但是你找到解决方法了吗?添加 MachineKey 似乎对我不起作用...
    • 不幸的是我没有找到任何解决方法,所以我在 IIS 上托管了我的 Web API,并在所有 Web 应用程序和 Web API 的 web.config 中设置了相同的 MachineKey 来解决问题。
    猜你喜欢
    • 2023-04-05
    • 1970-01-01
    • 2014-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-01
    • 1970-01-01
    相关资源
    最近更新 更多