views:

39

answers:

1

I have an administrator and a guest user...

When the administrator logs in he should be redirected to the default.aspx but if the guest logs in he should be redirected to the guest.aspx page... Currently it is being redirected to Default.aspx...

here is my code

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 code

 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));

            }

how do i get another URL for the guest....

any suggestions?? thanks..

A: 

In your guest section replace:

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

With:

// Redirect the user to the originally requested page 
FormsAuthentication.SetAuthCookie(UserName.Text, false)
Response.Redirect("guest.aspx", true); 
Kelsey
it is not able to get the the admin role in the default page///
with the Response.Redirect(FormsAuthentication.GetRedirectUrl(UserName.Text, false)); I was able to get the role of the user...
That should have said 'guest.aspx'. I assume your guests don't have any roles do they?
Kelsey
the guest are in guest role... i tried that but it was not getting the role...
I am not sure you can get the roles automatically using forms authentication. Wouldn't you need to use Windows Auth to make this information available? Sounds like you are trying to make a hybrid forms with windows auth?
Kelsey