如何从登录将guest重定向到guest.aspx

时间:2010-01-21 16:07:58

标签: c# forms .net-3.5 login response.redirect

我有一位管理员和来宾用户......

当管理员登录时,他应该被重定向到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));

            }

如何获取来宾的其他网址....

任何建议?感谢..

1 个答案:

答案 0 :(得分: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);