9

私たちには、ASP.NET MVC 4イントラネットアプリケーションがあります。私たちはWindows認証を使用しています。ユーザーの資格情報が使用され、Webアプリケーションから資格情報にアクセスできます。ASP.NET MVC 4でのWindows認証とフォーム認証のハイブリッド

私たちが本当に望むのは、ある種のハイブリッドモードです。ブラウザからユーザーの資格情報を取得する必要がありますが、ユーザーがアプリケーションのデータベースに存在することも確認したいと考えています。ユーザーがデータベースにいる場合は、そのまま続行できます。そうでない場合は、代替資格情報を要求するページにそれらをリダイレクトする必要があります。私が今やっていることは、にはApplication_AuthenticateRequestメソッドがあり、ユーザーが認証されているかどうかを確認しています。それらがあり、そのクッキー情報がシステムにログインしているという事実を反映していない場合、私はそれらをログインし、ユーザーに関する情報を含むいくつかのクッキーを設定します。認証されていない場合は、ログインページにリダイレクトします。会社のポリシーに関連する理由でADの役割を使用することはできないため、追加の認証にデータベースを使用する必要があります。

私はApplication_AuthenticateRequestがこれを行う場所ではないと推測していますが、多分そうです。しかし、基本的に認証の要求をフィルタリングする場所が必要です。しかし、この実装ではさらに別の問題が発生します。

匿名アクセスを許可する特定のURLがあります。これらのためにweb.configに<location>タグを追加しました。問題は、匿名の呼び出しがこれらに行われると、Application_AuthenticateRequestになり、ユーザーをDBにログインさせようとすることです。今、これらのURLを処理するためにApplication_AuthenticateRequestにコードを追加することはできますが、それは現在私の計画ですが、私が書いていてApplication_AuthenticateRequestがこれを行う場所ではない場合は、

答えて

5

この目的でアクションフィルタを使用する必要があります。私は、このソリューションが好き

[MyAuthorize] 
public ActionResult SomeAction() 
{ 
    // Code that is supposed to be accessed by authorized users only 
} 
+0

が、私はまだ上の401エラーを取得する:あなたはこのようにコントローラレベルまたはアクションのレベルでこの属性を使用することができ、その後

public class MyAuthorizeAttribute : AuthorizeAttribute { private UnitOfWork _unitOfWork = new UnitOfWork(); protected override bool AuthorizeCore(HttpContextBase httpContext) { var isAuthorized = false; var username = httpContext.User.Identity.Name; // Some code to find the user in the database... var user = _unitOfWork.UserRepository.Find(username); if(user != null) { isAuthorized = true; } return isAuthorized; } public override void OnAuthorization(AuthorizationContext filterContext) { if (filterContext == null) { throw new ArgumentNullException("filterContext"); } if (AuthorizeCore(filterContext.HttpContext)) { SetCachePolicy(filterContext); } else { // If not authorized, redirect to the Login action // of the Account controller... filterContext.Result = new RedirectToRouteResult( new System.Web.Routing.RouteValueDictionary { {"controller", "Account"}, {"action", "Login"} } ); } } protected void SetCachePolicy(AuthorizationContext filterContext) { // ** IMPORTANT ** // Since we're performing authorization at the action level, // the authorization code runs after the output caching module. // In the worst case this could allow an authorized user // to cause the page to be cached, then an unauthorized user would later // be served the cached page. We work around this by telling proxies not to // cache the sensitive page, then we hook our custom authorization code into // the caching mechanism so that we have the final say on whether a page // should be served from the cache. HttpCachePolicyBase cachePolicy = filterContext.HttpContext.Response.Cache; cachePolicy.SetProxyMaxAge(new TimeSpan(0)); cachePolicy.AddValidationCallback(CacheValidationHandler, null /* data */); } public void CacheValidationHandler(HttpContext context, object data, ref HttpValidationStatus validationStatus) { validationStatus = OnCacheAuthorization(new HttpContextWrapper(context)); } } 

を:あなたはこのようAuthorizeAttributeを拡張することができます誰にでも許可するコントローラメソッド。この特定のケースでは、私はC# 'HttpWebRequest.GetResponse()'呼び出しから呼び出しています。 'DebugController.FlushCaches()'には[AllowAnonymous]があり、 'web.config'には' 'を持つ' Debug/FlushCaches'の ''タグがあります。しかし、私の 'HttpWebRequest'はそれを呼び出したときに、私はあなたのタグとして401 – Pete

+0

使用これを取得: ' <場所のパスは= "デバッグ/ FlushCachesを"> <承認> ' – ataravati

+0

http://stackoverflow.com/questions/15087755/use-anonymous-authentication-in-mvc4-on-single-controller-when-the-whole-applica – ataravati

関連する問題