1

.net 4.0でアプリケーションを開発しています。私のWebアプリケーションプロジェクトでは、フォルダが保護されています。今、私は認証されたユーザーだけを保護されたページにリダイレクトしたいと思います。

IIS上に2つの異なる仮想ディレクトリを作成してこれを実行したい(もう1つのディレクトリ内の仮想ディレクトリ)

これはできますか?これどうやってするの?またはこれに何か方法はありますか?

よろしくお願いします。

4

1 に答える 1

0

コードで手動でそれを行いたい場合、適切な場所はトリガーにglobal.asaxありApplication_AuthenticateRequestます。これは例であり、ロジックに従って変更できます。

protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
    string cTheFile = HttpContext.Current.Request.Path;

    if(!cTheFile.Contains("/securedir/"))
    {
    if (HttpContext.Current.Request.IsSecureConnection)
    {
        HttpApplication app = (HttpApplication)sender;
        string cookieName = FormsAuthentication.FormsCookieName;
        HttpCookie authCookie = Context.Request.Cookies[cookieName];

        // if this is null, then the user is not logged in
        if (null == authCookie)
        {
  // for the same check you can also use
  //if(HttpContext.Current.User == null || HttpContext.Current.User.Identity == null || !HttpContext.Current.User.Identity.IsAuthenticated)

            string sExtentionOfThisFile = System.IO.Path.GetExtension(cTheFile);

            if (sExtentionOfThisFile.Equals(".aspx", StringComparison.InvariantCultureIgnoreCase))
            {
                Response.Redirect("/", true);
                Response.End();

                // There is no authentication cookie.
                return;
            }
        }
        }
    }
    }
}       
于 2012-10-02T07:17:13.677 に答える