【问题标题】:Authentication with Microsoft.Owin.Testing.TestServer for In-Memory Integration Tests使用 Microsoft.Owin.Testing.TestServer 进行内存集成测试的身份验证
【发布时间】:2013-11-22 17:51:43
【问题描述】:

我刚刚开始将 OWIN\Katana 用于 Web api 项目。它使用 Windows 身份验证。这似乎有效,但我的大多数集成测试都失败了。他们以前只是使用内存中的HttpServer,但我已经改为使用Microsoft.Owin.Testing.TestServer。我在我的测试设置中替换了这样的东西:

        var config = new HttpConfiguration { IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always };
        config.EnableQuerySupport();
        Server = new HttpServer(config);
        MyConfigClass.Configure(config);
        WebApiConfig.Register(config);

用更简单的:

TestServer = TestServer.Create<Startup>();

但是以前我可以将以下内容用于内存服务器的“假”身份验证:

Thread.CurrentPrincipal = new ClientRolePrincipal(new HttpListenerBasicIdentity(Username, Password));

这现在不起作用。对于所有请求,我都会收到以下信息:

System.Exception : {"Message":"Authorization has been denied for this request."}

如何使用 In-Memory OWIN 测试服务器进行身份验证或至少绕过身份验证?

【问题讨论】:

    标签: asp.net authentication asp.net-web-api owin katana


    【解决方案1】:

    我已经能够以一种我确信是次优的方式来解决这个问题,但在我遇到更好的解决方案或者你们中的一个人告诉我一个更好的方法来解决这个问题之前,我必须这样做:) 我做了如下:

    1. 在我的 Startup 类中,我添加了一个 CreateAuthFilter 挂钩,稍后我们将看到它仅用于集成测试:

      // Sample Startup class
      public class Startup
      {
          public void Configuration(IAppBuilder app)
          {
              var config = new HttpConfiguration();
      
              // Use CreateFilter Method to create Authorisation Filter -  if not null add it
              var authFilter = CreateAuthFilter();
              if(authFilter != null)
                  config.Filters.Add(authFilter);
      
              // Other configuration and middleware...
          }
      
          public static Func<IFilter> CreateAuthFilter = () => null;
      }
      
    2. 实现了一个仅用于集成测试的授权过滤器:

      public class TestAuthFilter : IAuthenticationFilter
      {
          static TestAuthFilter()
          {
              TestUserId = "TestDomain\\TestUser";
          }
      
          public bool AllowMultiple { get; private set; }
      
          public async Task AuthenticateAsync(HttpAuthenticationContext context, CancellationToken cancellationToken)
          {
              context.Principal = new ClientRolePrincipal(new HttpListenerBasicIdentity(TestUserId, "password")); ;
          }
      
          public static string TestUserId { get; set; }
      
          public async Task ChallengeAsync(HttpAuthenticationChallengeContext context, CancellationToken cancellationToken)
          {
      
          }
      }
      
    3. 在我的集成测试的设置代码中,我注入了测试授权过滤器:

      Startup.CreateAuthFilter = () => new TestAuthFilter();
      var TestServer = TestServer.Create<Startup>();
      
    4. 在特定测试中需要时,我将 TestUserId 设置为已知值,而其他测试似乎只是工作,因为存在 Auth 过滤器:

      TestAuthFilter.TestUserId = testUser.UserId;
      

    我在这里分享这个,以防它帮助其他人,但请有人告诉我一个更好的方法!至少我确信有更好的方法来注入我的测试过滤器而不在 Startup 中包含代码......我只是还没想到。

    【讨论】:

    • 你能开源一个演示使用 OWIN 进行集成测试的项目吗?
    • 当然,我会看看我是否可以在接下来的几天内将一些简单的东西放在一起,并在我这样做时使用链接编辑上面的答案。如果我在几天内没有这样做,你可以给我发电子邮件提醒吗? simon1979 (at) 来自 google 的邮件。
    • 天才。谢谢你 - 它让我疯狂地试图弄清楚如何让模拟用户出现在上下文中。我选择了 MVC 通常分配的 GenericPrinicpal 而不是 ClientRolePrincipal。
    猜你喜欢
    • 1970-01-01
    • 2013-11-22
    • 2013-03-18
    • 1970-01-01
    • 2020-08-04
    • 2017-02-25
    • 1970-01-01
    • 2016-04-12
    • 2017-11-12
    相关资源
    最近更新 更多