2012-10-25 35 views
10

我们正在构建将使用基本身份验证的ServiceStack API。我目前正在建立权威性在我APPHOST如下:当ServiceStack身份验证失败时,请不要重定向?

var authDb = new OrmLiteConnectionFactory("Server=(...);", true, MySqlDialectProvider.Instance); 

var authRepo = new OrmLiteAuthRepository(authDb); 
authRepo.CreateMissingTables(); 
container.Register<ICacheClient>(c => new MemoryCacheClient()); 
container.Register<IUserAuthRepository>(c => authRepo); 

Plugins.Add(
    new AuthFeature(() => new AuthUserSession(), new IAuthProvider[] { new BasicAuthProvider() }) 
); 

在做,没有授权头或错误的用户名+通过响应的请求被重定向到/Account/Login.aspx?ReturnUrl= ...

Parital请求响应+例如:

POST http://localhost:60278/reserve HTTP/1.1 

HTTP/1.1 302 Found 
Location: /Account/Login.aspx?ReturnUrl=%2freserve 
X-Powered-By: ServiceStack/3,924 Win32NT/.NET 

有没有一种方法,使之401未授权或HTTP 403禁止只用HTTP响应?

回答

22

默认情况下ServiceStack的AuthFeature将只尝试将您重定向到默认~/login路径HTML内容类型的请求。您可以通过在AuthFeature设置为null重定向路径覆盖此:

Plugins.Add(new AuthFeature(...) { HtmlRedirect = null }); 

这将回落到标准401 UnAuthorized响应,其他的内容类型得到。

全局设置HtmlRedirect为空后,您可以将其重新添加在临时基础上,e.g:

[Authenticate(HtmlRedirect="~/path/to/redirect/to")] 
+0

我认为必须有一些ASP.net的东西在这里混淆了我。我在文档中看到它,并尝试了HtmlRedirect = null,并且我使用application/json作为Accept/Content-type标头。重定向与您写入(〜/ login)不同,但是/Account/Login.aspx。我会通过我的global.aspx和东西,让你知道。 我现在接受你的回答,不过,这是SS正确设置时的解决方案。谢谢! :) – Magge

+0

是的,这是我的不好。我的Web.config中仍然有.net成员身份,似乎覆盖了SS身份验证。我删除它之后就像一个魅力。 – Magge

+0

我懂了! ... :) –