2010-01-21 73 views
0

我有一個管理員和來賓用戶guest.aspx ...如何重定向客人從登錄

當他的管理員日誌應該在他應該被重定向到Default.aspx但如果客日誌被重定向到guest.aspx頁......目前,它正在被重定向到Default.aspx的...

這裏是我的代碼

的web.config

authentication mode="Forms"> 
    <forms loginUrl="Login.aspx" protection="All" name="Cookie" timeout="120" path="/" slidingExpiration="true" 
     defaultUrl="Default.aspx"> 
    </forms> 
    </authentication> 

Login.cs碼

System.Security.Principal.WindowsIdentity wi = System.Security.Principal.WindowsIdentity.GetCurrent(); 
       System.Security.Principal.WindowsPrincipal wp = new System.Security.Principal.WindowsPrincipal(wi); 


       if (wp.IsInRole("Administrators")) 
       { 
        BadCredentials.Visible = false; 
        Session["userName"] = UserName.Text; 
        Session["password"] = Password.Text; 
        Session["domain"] = Domain.Text; 


        string role = "Administrators"; 

        // Create the authentication ticket 
        FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1,       // version 
                UserName.Text,   // user name 
                DateTime.Now,    // creation 
                DateTime.Now.AddMinutes(60),// Expiration 
                false,      // Persistent 
                role);   // User data 

        // Now encrypt the ticket. 
        string encryptedTicket = FormsAuthentication.Encrypt(authTicket); 
        // Create a cookie and add the encrypted ticket to the 
        // cookie as data. 
        HttpCookie authCookie = 
           new HttpCookie(FormsAuthentication.FormsCookieName, 
               encryptedTicket); 

        // Add the cookie to the outgoing cookies collection. 
        Response.Cookies.Add(authCookie); 

        // Redirect the user to the originally requested page 
        Response.Redirect(FormsAuthentication.GetRedirectUrl(UserName.Text, false)); 

       } 
       else if (wp.IsInRole("Guests")) 
       { 
         BadCredentials.Visible = false; 
       Session["userName"] = UserName.Text; 
       Session["password"] = Password.Text; 
       Session["domain"] = Domain.Text; 

       string role = "Guests"; 

       // Create the authentication ticket 
       FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1,       // version 
               UserName.Text,   // user name 
               DateTime.Now,    // creation 
               DateTime.Now.AddMinutes(60),// Expiration 
               false,      // Persistent 
               role);   // User data 

       // Now encrypt the ticket. 
       string encryptedTicket = FormsAuthentication.Encrypt(authTicket); 
       // Create a cookie and add the encrypted ticket to the 
       // cookie as data. 
       HttpCookie authCookie = 
          new HttpCookie(FormsAuthentication.FormsCookieName, 
              encryptedTicket); 

       // Add the cookie to the outgoing cookies collection. 
       Response.Cookies.Add(authCookie); 

       // Redirect the user to the originally requested page 
       Response.Redirect(FormsAuthentication.GetRedirectUrl(UserName.Text, false)); 

      } 

如何獲得另一個URL爲客人....

什麼建議?感謝..

+0

如果你總是想重定向到一個特定的頁面,你爲什麼在這兩種情況下重定向到FormsAuthentication.GetRedirectUrl (UserName.Text,false)。您可以使用Response.Redirect(adminPage)或Response.Redirect(guestPage)爲變量提供所需的值。我已發佈爲評論,因爲我猜我讀的問題不同於你的意圖 – 2010-01-21 16:15:19

+0

我以爲FormsAuthentication.GetRedirectUrl(UserName.Text,false);需要發送的用戶名令牌密鑰生成......但我不知道。我還以爲GetRedirectUrl得到ü存儲在某個字符串變量所需的網址... – user175084 2010-01-21 16:20:05

回答

0

在您的客戶部分取代:

// Redirect the user to the originally requested page 
Response.Redirect(FormsAuthentication.GetRedirectUrl(UserName.Text, false)); 

有了:

// Redirect the user to the originally requested page 
FormsAuthentication.SetAuthCookie(UserName.Text, false) 
Response.Redirect("guest.aspx", true); 
+0

是不能夠得到的管理角色默認頁面/// – user175084 2010-01-21 16:36:38

+0

與Response.Redirect(FormsAuthentication.GetRedirectUrl(UserName.Text,false)); 我能得到用戶... – user175084 2010-01-21 16:39:36

+0

這應該說「guest.aspx」的作用。我假設你的客人沒有任何角色嗎? – Kelsey 2010-01-21 16:42:16