コード例 #1
0
    protected void LogInButton_Click(object sender, EventArgs e)
    {
        try
        {
            //Create object of Crypto class
            CryptoClass crypto = new CryptoClass();
            String      id     = UserNameTb.Text;
            //Encrypt the password
            String password = crypto.Encrypt(PasswordTb.Text);

            //Create object of reference class
            Assignment5ServiceRefs.Service1Client serviceRef = new Assignment5ServiceRefs.Service1Client();
            //get the role from the Staff.xml
            string role = serviceRef.checkUserCredentialsForStaff(id, password);
            //if the role is not empty
            if (!role.Equals(""))
            {
                //Create a cookie and store the user id
                HttpCookie userCookies = new HttpCookie("userInfo");
                userCookies["id"] = UserNameTb.Text;
                //Cookie expires in 6 months
                userCookies.Expires = DateTime.Now.AddMonths(6);
                Response.Cookies.Add(userCookies);
                //Redirect to the staff page after authentication
                FormsAuthentication.RedirectFromLoginPage(UserNameTb.Text, Persistent.Checked);
                //Store the role in the session
                Session["type"]     = role;
                userCookies["type"] = role;
                //If the role is staff, redirect to the staff page
                if (role.Equals("staff"))
                {
                    Response.Redirect("~/StaffPage2/StaffPage.aspx");
                }
                //else if the role is admin, redirect to the admin page
                else if (role.Equals("admin"))
                {
                    Response.Redirect("~/StaffPage1/AdminPage.aspx");
                }
                //else redirect to the public default page
                else
                {
                    Response.Redirect("~/Default.aspx");
                }
            }
            //Print error message if the credentials are invalid
            else
            {
                LoginErrorLbl.Text = "Invalid credentials!";
            }
        }
        catch
        {
            LoginErrorLbl.Text = "Invalid credentials!";
        }
    }