2010-07-26 32 views
0

我在我的網站中使用內置的asp.net角色和成員資格提供程序。如何將用戶從登錄頁面重定向到下一頁基於他們在Asp.net中的角色C#

當用戶登錄到我的系統時,他們被重定向到了hompage。

我該如何編碼。當他點擊登錄按鈕頁面時,檢查它的角色,然後決定在哪裏重定向。 假設用戶名稱爲John且「john」爲「Admin」,那麼應用程序將他重定向到AdminPanel.aspx,如果用戶john是正常的「RegUser」角色,則將其重定向到Home.aspx。

在此先感謝........

回答

1

在您的登錄頁面,如果用戶成功登錄後,你可以存儲在Session和他們的角色你Home.aspx只是有一點邏輯這決定了他們是否需要重新定向離開這個頁面或沒有,例如

login.aspx的

void LoginBtn_Click(Object sender, EventArgs e) 
{ 
    // have a function which returns the user object if successful, otherwise return null 
    User user = DoLogin(txtUsername.Text, txtPassword.Text); 
    if (user != null) 
    { 
     Session["UserRole"] = user.RoleName; 
     // if you aren't using the authentication stuff from the web.config then 
     // then you will need to manually redirect the user 
    } 
} 

home.aspx

void Page_Load(Object sender, EventArgs e) 
{ 
    string role = !String.IsNullOrEmpty(Session["UserRole"]) ? (string)Session["UserRole"] : String.Empty; 
    if (role == "ADMIN") 
     Response.Redirect("adminpanel.aspx"); 
} 
相關問題