2012-10-25 65 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

我懂了! ... :) –