【解決方法】ログインページにリダイレクトする方法


こんにちは、みんな 、

ログインURLコードについて知りたいです。

私のプロジェクトでは、ログインページがあり、mozilla にサインインして URL をコピーし、それを chrome や IE のような他の Web サイトに貼り付けます。ホームページ (ログイン ページ) を表示するには、ログイン ページをリダイレクトする必要があります。

助けて…

解決策 2

この記事を読む:

ASP.NET メンバーシップとロール プロバイダー[^]

また

このコードを web.config ファイルで使用します

XML
<authentication mode="Forms">
     <forms cookieless="UseCookies" defaultUrl="HomePage.aspx"
    loginUrl="UnAuthorized.aspx" protection="All" timeout="30">
          </forms>
</authentication>

次のコードでログイン制御イベントを使用します。

C#
protected void Login1_Authenticate(object sender,AuthenticateEventArgs e)
{
 if (Membership.ValidateUser(Login1.UserName, Login1.Password) == true)
    {
        Login1.Visible = true;
        Session["user"] = User.Identity.Name;
        FormsAuthentication.RedirectFromLoginPage(Login1.UserName, true);
    }
 else
    {
        Response.Write("Invalid Login");
    }
}

解決策 3

解決策 7

ありがとうございます。私は以下のように使用しています。うまく機能しています。

C#
        protected void Application_Start(object sender, EventArgs e)
        {

        }

        protected void Session_Start(object sender, EventArgs e)
        {
            Response.Redirect(Request.Url.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute("~\\Home.aspx"));
        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {
            Response.Redirect(Request.Url.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute("~\\Home.aspx"));
        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}

解決策 1

ユーザーがログインするときにセッション変数を作成する必要があります。この変数はおそらく Session[“UserName”]. すべてのページの Page_Load で、null の変数をチェックし、それ以外の場合はユーザーをログイン ページにリダイレクトする必要があります。

C#
if(Session["UserName"]==null)
{
Response.Redirect("Login.aspx");
}

このコードをすべてのページに記述するのは不便なので、ベース ページに記述します。

ベースページのコンセプトはこちら

C#
public class SessionCheck : System.Web.UI.Page
{
    override protected void OnInit(EventArgs e)
    {
        //initialize our base class (System.Web,UI.Page)
        base.OnInit(e);
        //check to see if the Session is null (doesnt exist)
        if (Context.Session != null)
        {
if(Session["UserName"]==null)
{
Response.Redirect("Login.aspx");
}
            }
    }
}

解決策 5

コメント

タイトルとURLをコピーしました