コード例 #1
0
        void loginButton_Click(object sender, EventArgs e)
        {
            String username = usernameBox.Text.Trim();
            String password = passwordBox.Text;

            //int stationId = Int32.Parse(stationList.SelectedValue);


            StationTableAdapter stationAdapter = new StationTableAdapter();

            String stationCode = stationCodeBox.Text.Trim().ToUpper();

            int stationId = 0;

            if (stationCode != String.Empty)
            {
                DollarSaverDB.StationDataTable stationLookup = stationAdapter.GetByCode(stationCode);

                if (stationLookup.Count != 1 || !stationLookup[0].IsActive)
                {
                    errorMessage = "Incorrect username, password or station";
                    return;
                }

                stationId = stationLookup[0].StationId;
            }


            AdminTableAdapter adminAdapter = new AdminTableAdapter();

            if ((int)adminAdapter.Authenticate(stationId, username, password) == 1)
            {
                DollarSaverDB.AdminRow user = adminAdapter.GetByUsername(stationId, username)[0];

                user.LastAccessDate = DateTime.Now;
                adminAdapter.Update(user);

                int userStationId;
                if (user.Role == AdminRole.Root)
                {
                    userStationId = 0;
                }
                else
                {
                    userStationId = stationId;
                }
                Session["admin_station_id"] = userStationId;

                HttpCookie cookie = Request.Cookies.Get(ADMIN_COOKIE_NAME);

                if (cookie == null)
                {
                    cookie = new HttpCookie(ADMIN_COOKIE_NAME);
                }

                cookie.Expires = DateTime.Now.AddYears(10);
                if (IsDev)
                {
                    cookie.Domain = EnvDomain;
                }
                else
                {
                    cookie.Domain = ".dollarsavershow.com";
                }

                cookie["station_id"]   = userStationId.ToString();
                cookie["station_code"] = stationCode;

                HttpContext.Current.Response.Cookies.Add(cookie);

                FormsAuthentication.SetAuthCookie(user.AdminId.ToString(), true);

                if (user.Role == AdminRole.Root)
                {
                    if (stationId == 0)
                    {
                        Response.Redirect("~/admin/super/", false);
                    }
                    else
                    {
                        Response.Redirect("~/admin/Default.aspx?station_id=" + stationId, false);
                    }
                }
                else
                {
                    Response.Redirect("~/admin/", false);
                }
            }
            else
            {
                errorMessage = "Incorrect username, password or station";
            }
        }