我刚刚将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."}
如何使用内存中的OWIN测试服务器进行身份验证,或者至少绕过身份验证?
发布于 2013-11-12 11:11:15
我已经能够以一种我确信不是最优的方式来解决这个问题,但在我遇到更好的解决方案或者你们中的某个人告诉我更好的方法之前,我将不得不这样做:)我已经做到了以下几点:
//示例启动类公共类启动{公共空配置(IAppBuilder app) { var配置=新的HttpConfiguration();//使用CreateFilter方法创建授权筛选器-如果不为null,则添加它var authFilter = CreateAuthFilter();if(authFilter != null) config.Filters.Add(authFilter);//其他配置和中间件...}公共静态函数CreateAuthFilter = () => null;}
公共类TestAuthFilter : IAuthenticationFilter { static TestAuthFilter() { TestUserId =“测试域\测试用户”;}公共布尔AllowMultiple { get;私有set;}公共异步任务AuthenticateAsync(HttpAuthenticationContext context,CancellationToken cancellationToken) { context.Principal =新密码(HttpAuthenticationContext HttpListenerBasicIdentity( TestUserId,"password"));;}公共静态字符串TestUserId{ get;set;}公共异步任务ChallengeAsync(HttpAuthenticationChallengeContext context,CancellationToken cancellationToken) {}}
var Startup.CreateAuthFilter = () => new TestAuthFilter();var TestServer =var在特定的测试中,我将TestUserId设置为一个已知值,而其他测试似乎都能正常工作,因为存在Auth过滤器:
TestAuthFilter.TestUserId = testUser.UserId;
我在这里分享这个,以防对其他人有帮助,但请谁告诉我一个更好的方法!至少,我确信有一种更好的方法来注入我的测试过滤器,而不需要在启动中包含代码……我只是还没想到这一点。
https://stackoverflow.com/questions/19897705
复制相似问题