簡體   English   中英

如何在基於表單的身份驗證中檢查用戶是否已登錄

[英]How to check if user is logged in or not in forms based authentication

我想在我的網站上手動實現基於表單的身份驗證。

我正在使用Web.config文件進行數據存儲

<authentication mode="Forms">
  <forms loginUrl="~/Login.aspx" 
         name=".ASPXAUTH"
         path="/"
         requireSSL="false"
         slidingExpiration="true"
         defaultUrl="~/Admin/OrderHistory.aspx"
         cookieless="UseDeviceProfile"
         enableCrossAppRedirects="false"
         >
    <credentials passwordFormat="Clear">
      <user name="Admin" password="adm123$"/>
      <user name="Administrator" password="adm234%"/>
    </credentials>
  </forms>
</authentication>
<authorization>
  <deny users ="?" />
  <allow users = "*" />
</authorization>

在根目錄下有一個Login.aspx頁面,使用ASP.NET登錄控件來獲取用戶名和密碼。

一切正常,但當用戶logged in並手動轉到login.aspx頁面時,它不會將用戶重定向到defaultUrl頁面。

我想將用戶重定向到特定頁面/ defaultUrl頁面,如果他已登錄並手動登錄到login.aspx頁面

怎么做?

登錄按鈕 - 單擊

if (FormsAuthentication.Authenticate(LoginUser.UserName, LoginUser.Password))
    {
        FormsAuthentication.RedirectFromLoginPage(LoginUser.UserName, true);

    }
    if(HttpContext.Current.User.Identity.IsAuthenticated)
    {

    //Redirect to Default page
    Response.Redirect("default.aspx");
    }

我通常會使用會話策略。

如果建立了會話,則將用戶重定向到特定頁面,或者指示他登錄。

您可以在這里查詢更多信息http://asp.net-tutorials.com/state/sessions/

在web.config文件中使用forms屬性defaulturl =“xyz.aspx”(您的URL)

 protected void Page_Load(object sender, EventArgs e)
    {  
        if (!Request.UrlReferrer.Equals("http://localhost:1360/login_page.aspx"))
        {
            Response.Redirect("ERROR ==>? 404");
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM