MVC 3 サイトで WS Federated (Claims Aware) 認証を使用していますが、JSON を送信する一部の API コントローラーが認証に失敗したときにリダイレクトを返さないようにするのに問題があります。JSON を返すだけのいくつかのコントローラーを備えた API と呼ばれるエリアがあり、これらのコントローラーはすべて同じ基本クラスから継承されます。デフォルトで発生する 302 リダイレクトの代わりに、正当な 401 エラー応答を送信したいと考えています。
WSFederationAuthenticationModule
APIコントローラーアクションに適用したフィルターと連携してカスタムを作成するために見つけたいくつかの指示に従いました。
public class WSFederationServiceAuthenticationModule : WSFederationAuthenticationModule
{
private static Log4NetLoggingService logger = new Log4NetLoggingService();
public const string IsServiceIndicator = "ROIP.IsService";
protected override void OnAuthorizationFailed(AuthorizationFailedEventArgs e)
{
base.OnAuthorizationFailed(e);
var isService = HttpContext.Current.Items[IsServiceIndicator];
if (isService != null)
{
logger.Info("WSFedService: Found IsService");
e.RedirectToIdentityProvider = false;
}
else
{
logger.Info("WSFedService: Did not find IsService");
}
}
}
public class WSFederationServiceAuthAttribute : ActionFilterAttribute
{
private static Log4NetLoggingService logger = new Log4NetLoggingService();
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
// Set an item that indicates this is a service request, do not redirect.
logger.Info("WSFedService: Setting IsService");
HttpContext.Current.Items[WSFederationServiceAuthenticationModule.IsServiceIndicator] = 1;
}
}
しかし、私のログには、アイテムに IsService アイテムが見つからないことが示されています。
{INFO}02/29 03:39:21 - WSFedService: Setting IsService
{INFO}02/29 03:39:32 - WSFedService: Setting IsService
{INFO}02/29 03:39:32 - WSFedService: Setting IsService
{INFO}02/29 03:50:39 - WSFedService: Did not find IsService
{INFO}02/29 03:53:16 - WSFedService: Did not find IsService
{INFO}02/29 03:53:29 - WSFedService: Did not find IsService
これは、フィルターとモジュールが同じではないことに問題があるのではないかと思いますHttpContext.Current
が、よくわかりません。
私が試した別のオプションは、Global.asax.csFederatedAuthentication.WSFederationAuthenticationModule.RedirectingToIdentityProvider
のイベントでイベントをサブスクライブすることでしたApplication_Start
が、その時点で WSFederationAuthenticationModule は null です。
private void ConfigureWSFederationAuthentication()
{
bool hasFederatedAuthentication = false;
try
{
if (FederatedAuthentication.WSFederationAuthenticationModule != null)
{
hasFederatedAuthentication = true;
}
}
catch
{
hasFederatedAuthentication = false;
}
if (hasFederatedAuthentication)
{
Logger.Info("WSFederation: Registering for Event Handler");
FederatedAuthentication.WSFederationAuthenticationModule.RedirectingToIdentityProvider += (s, e) =>
{
var msg = string.Empty;
try
{
if (HttpContext.Current.Request.Headers["X-Requested-With"] == "XMLHttpRequest")
{
e.Cancel = true;
msg = "Found XMLHttpRequest header";
}
else
{
msg = "Did not find XMLHttpRequest header";
}
}
catch (Exception ex)
{
msg = "WSFederation: Event Handler Error: " + ex.Message;
}
Logger.Info("WSFederation: Redirecting from Event Handler: " + msg);
};
}
else
{
Logger.Info("WSFederation: Null WSFederationAuthenticationModule");
}
}
RedirectingToIdentityProvider
最初のオプションを機能させる方法、またはイベントを購読する場所を知りたいです。