Example #1
0
    protected void ButLogin_Click(object sender, EventArgs e)
    {
        try
        {
            int?   userId;
            string connectionString            = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["CustomerRecoveryConnectionString"].ConnectionString;
            SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connectionString);
            string name = builder.InitialCatalog;
            CRBusinessLogicLayer.IsValidUser(txtUserName.Text, txtPassword.Text, out userId);
            if (userId == null)
            {
                txtUserName.Text = "";
                txtPassword.Text = "";
                LbError.Text     = "Invalid  User Name/Password.";
                LbError.Visible  = true;
            }
            else
            {
                var    ds                        = CRBusinessLogicLayer.GetSequerityQuestion((int)userId);
                string SessionuserID             = name + "_" + "userid";
                var    UserType                  = ds.Tables[0].Rows[0]["UserRole"].ToString();
                var    CityID                    = ds.Tables[0].Rows[0]["City"].ToString();
                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, txtUserName.Text, DateTime.Now, DateTime.Now.AddDays(7), true, String.Format("{0}|{1}|{2}", txtUserName.Text, userId, UserType));
                string hash                      = FormsAuthentication.Encrypt(ticket);
                FormsAuthentication.SetAuthCookie(txtUserName.Text, true);
                Response.AppendCookie(new HttpCookie(SessionuserID, userId.ToString()));
                Response.AppendCookie(new HttpCookie("UserName", txtUserName.Text));
                Response.AppendCookie(new HttpCookie("UserRole", UserType));
                System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
                Byte[] bytes = encoding.GetBytes(txtPassword.Text);
                string pwd   = CRBusinessLogicLayer.PassEncrypt(bytes);
                Response.AppendCookie(new HttpCookie("Pass", pwd));
                Response.AppendCookie(new HttpCookie("CityID", CityID));
                Response.AppendCookie(new HttpCookie("USERID", userId.ToString()));
                Response.AppendCookie(new HttpCookie("udata", hash));

                string url = string.Empty;
                ds = CRBusinessLogicLayer.GetUserAccessURL((int)userId);
                if (ds.Tables[0].Rows.Count == 1)
                {
                    url = ds.Tables[0].Rows[0]["ScreenURL"].ToString();
                }
                else if (url == string.Empty)
                {
                    Response.Redirect(
                        Convert.ToInt16(UserType) == 2
                            ? "~/DailyTransactions/NPADetails.aspx"
                            : "~/Masters/Farmer.aspx", false);
                }
                if (url != string.Empty)
                {
                    Response.Redirect(url, false);
                }
            }
        }
        catch (Exception ex)
        {
            LbError.Text    = ex.Message;
            LbError.Visible = true;
        }
    }