(古い質問に返信しますが、他の人に役立つかもしれません)
基本認証またはケルベロス認証のみをサポートする Web API の前にファサードを配置する必要があるという同様の問題がありました。
受信 URL をバックエンド サーバーの URL に変換するこのソリューションを使用しました: http://www.dotnetspeak.com/asp-net-mvc/using-webapi-in-multi-tier-web-application/
Authorization フィルターの前に ExecuteAsync が実行されるため、ヘッダー内の (asp.net ログイン システムからの) トークンをチェックするロジックをそのソリューションに追加しました。
var token = controllerContext.Request.Headers.Authorization;
if (token != null && token.Scheme.Equals("bearer", StringComparison.InvariantCultureIgnoreCase))
{
var ticket = Startup.OAuthOptions.AccessTokenFormat.Unprotect(token.Parameter);
if (ticket != null && ticket.Identity != null && ticket.Identity.IsAuthenticated)
{
var claimsPrincipal = new ClaimsPrincipal(ticket.Identity);
//From here, you can use the claimsPrinciple to check if user is allowed to even call the service.
var authorized = claimsPrincipal.IsInRole("Users");
}
}
Startup.OAuthOptions が使用できない場合は、これを Startup.cs または Startup.Auth.cs の静的変数に変換する必要がある場合があります。
バックエンド サービスの基本認証の代わりに別の認証方法を提供する必要があったため、基本認証に切り替えるためにヘッダーの追加の更新が追加されています。
//from the dotnetspeak solution (copy existing headers)
foreach (var httpRequestHeader in controllerContext.Request.Headers)
{
client.DefaultRequestHeaders.Add(httpRequestHeader.Key, httpRequestHeader.Value);
}
//Set basic authentication, whatever the original Authorization header might have been
//TODO: use lookup table or something like that to convert claimsPrinciple to matching domain user account
var byteArray = Encoding.ASCII.GetBytes(@"Domain\userId:password");
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));