private void cmdLogin_Click(object sender, System.EventArgs e)
        {
            // Check if the control values are valid.
            if (Page.IsValid)
            {
                // Uncomment the line for the credential store you want to use.
                //ICredentialStore store = new DefaultCredentialStore();
                //ICredentialStore store = new XmlCredentialStore(Server.MapPath("Users.xml"));
                ICredentialStore store = new DatabaseCredentialStore("CredentialConnectionString", "SHA1");

                // Non-user data version.
                //				if (store.Authenticate(txtName.Text, txtPassword.Text))
                //				{
                //					lblStatus.Text = "Logged in.";
                //
                //					// Log the user in, create the cookie, and redirect to the original page.
                //					FormsAuthentication.RedirectFromLoginPage(txtName.Text, false);
                //				}
                //				else
                //				{
                //					// Show an error message.
                //					lblStatus.Text = "Try again.";
                //				}
                // User data version.

                string userData;
                if (store.Authenticate(txtName.Text, txtPassword.Text, out userData))
                {
                    lblStatus.Text = "Logged in.";

                    // Create a new authentication ticket.
                    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                        1,                                                   // Version
                        txtName.Text,                                        // User name
                        DateTime.Now,                                        // Date issued
                        DateTime.Now.AddMinutes(30),                         // Date to expire
                        false,                                               // Persistent
                        userData);                                           // User data string

                    // Encrypt the ticket.
                    string encryptedTicket = FormsAuthentication.Encrypt(ticket);

                    // Create the authentication cookie.
                    HttpCookie authenticationCookie = new
                                                      HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);

                    // Attach the cookie to the response.
                    Response.Cookies.Add(authenticationCookie);

                    // Redirect the user back to their original URL.
                    Response.Redirect(FormsAuthentication.GetRedirectUrl(
                                          txtName.Text, false));
                }
                else
                {
                    // Show an error message.
                    lblStatus.Text = "Try again.";
                }
            }
        }
Ejemplo n.º 2
0
        private void cmdLogin_Click(object sender, System.EventArgs e)
        {
            // Check if the control values are valid.
            if (Page.IsValid)
            {
                ICredentialStore store = new DatabaseCredentialStore("CredentialConnectionString", "SHA1");
                if (store.Authenticate(txtName.Text, txtPassword.Text))
                {
                    lblStatus.Text = "Logged in.";

                    // Get the role information.
                    string[] roles = store.GetRoles(txtName.Text);

                    // Convert the roles to a single string,
                    // so it can be attached to the cookie.
                    string roleList = string.Join("%", roles);

                    // Create a new authentication ticket.
                    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                        1,                                                   // Version
                        txtName.Text,                                        // User name
                        DateTime.Now,                                        // Date issued
                        DateTime.Now.AddMinutes(30),                         // Date to expire
                        false,                                               // Persistent
                        roleList);                                           // User data string

                    // Encrypt the ticket.
                    string encryptedTicket = FormsAuthentication.Encrypt(ticket);

                    // Create the authentication cookie.
                    HttpCookie authenticationCookie = new
                                                      HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);

                    // Attach the cookie to the response.
                    Response.Cookies.Add(authenticationCookie);

                    // Redirect the user back to their original URL.
                    Response.Redirect(FormsAuthentication.GetRedirectUrl(
                                          txtName.Text, false));
                }
                else
                {
                    // Show an error message.
                    lblStatus.Text = "Try again.";
                }
            }
        }