10

基本認証を使用する ServiceStack API を構築しています。現在、次のように AppHost で認証を設定しています。

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() })
);

Authorization ヘッダーがないか、ユーザー名とパスが間違っている要求を実行すると、応答は /Account/Login.aspx?ReturnUrl=... へのリダイレクトになります。

部分的なリクエスト + レスポンスの例:

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

HTTP 401 Unauthorized または HTTP 403 Forbidden のみで応答させる方法はありますか?

4

2 に答える 2

22

デフォルトでは、ServiceStack の AuthFeature は、 HTMLコンテンツ タイプ リクエストのデフォルト~/loginパスへのリダイレクトのみを試みます。これは、AuthFeature のリダイレクト パスを null に設定することでオーバーライドできます。

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

401 UnAuthorizedこれは、他の Content-Types が取得する標準の Response にフォールバックします。

HtmlRedirect をグローバルに null に設定した後、アドホック ベースで追加し直すことができます。

[Authenticate(HtmlRedirect="~/path/to/redirect/to")]
于 2012-10-25T16:38:33.230 に答える