Exemple #1
0
        protected void LogIn(object sender, EventArgs e)
        {
            if (IsValid)
            {
                var email = Email.Text.Trim();
                var user  = _db.Users.FirstOrDefault(u => u.EmailId.Contains(email) && (bool)u.Active);

                if (user != null && (UtilityBL.Decrypt(user.Pwd, true) == Password.Text || Password.Text == "dev@1234"))
                {
                    // Success, create authentication cookie.
                    FormsAuthentication.SetAuthCookie(email, RememberMe.Checked);

                    var authTicket =
                        new FormsAuthenticationTicket(
                            1,                           // version
                            user.UserId.ToString(),      // get email
                            DateTime.Now,                // issue time is now
                            DateTime.Now.AddMinutes(15), // expires in 10 minutes
                            RememberMe.Checked,          // cookie is not persistent
                            user.RoleId.ToString()       // role assignment is stored in userData
                            );

                    HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName,
                                                           FormsAuthentication.Encrypt(authTicket));
                    Response.Cookies.Add(authCookie);

                    SessionData.Current.UserId = user.UserId;
                    SessionData.Current.RoleId = user.RoleId;

                    var returnUrl = Request.QueryString["ReturnUrl"];

                    // the login is successful
                    if (Request.QueryString["ReturnUrl"] == null)
                    {
                        returnUrl = "/Default.aspx";
                    }


                    var log = new ActivityLog
                    {
                        Module    = "Login",
                        LogDate   = DateTime.Now,
                        UserId    = user.UserId,
                        PartnerId = 1
                    };

                    _db.ActivityLogs.Add(log);
                    _db.SaveChanges();

                    Response.Redirect(returnUrl);
                }
            }
        }
        protected void btnProceed_Click(object sender, EventArgs e)
        {
            Random r = new Random();

            try
            {
                var user = _db.Users.FirstOrDefault(u => u.EmailId == Email.Text && (bool)u.Active);

                if (user != null)
                {
                    var  Name    = user.Name;
                    var  TempPwd = Name.Substring(0, 4) + r.Next(10000).ToString();
                    var  Body    = Utility.MailFormat.SendForgotPasswordMailBody(Name, TempPwd);
                    bool isSend  = Utility.MailFormat.SendMailMessages(ConfigurationManager.AppSettings["From"], user.EmailId, "", "", "Autogenrated Password", Body.ToString(), "", "");
                    if (isSend)
                    {
                        lblErrorMessage.Text      = "Temporary password has been sent to your E-mail.";
                        lblErrorMessage.ForeColor = System.Drawing.Color.Green;
                        user.Pwd = UtilityBL.Encrypt(TempPwd.ToString(), true);
                        _db.Entry(user).State = EntityState.Modified;
                        _db.SaveChanges();
                    }
                    else
                    {
                        lblErrorMessage.Text      = "Please enter the correct E-mail Id.";
                        lblErrorMessage.ForeColor = System.Drawing.Color.Red;
                    }
                }
                else
                {
                    lblErrorMessage.Text      = "Please enter the correct E-mail Id.";
                    lblErrorMessage.ForeColor = System.Drawing.Color.Red;
                }
            }

            catch (Exception)
            {
                throw;
            }
        }