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