Beispiel #1
0
        public ActionResult DefaultLogin(LoginModels objLogin)
        {
            try
            {
                Response.Cookies["RememberMe"].Value = objLogin.rememberMe.ToString();
                if (objLogin.rememberMe)
                {
                    var userencrypt = string.Join(",", objLogin.userName, objLogin.password, objLogin.rememberMe);
                    Response.Cookies["rem_user"].Expires = DateTime.Now.AddDays(30);
                    Response.Cookies["rem_user"].Value   = objLogin.Encryptdata(userencrypt);

                    Response.Cookies["RememberMe"].Expires = DateTime.Now.AddDays(30);
                    Response.Cookies["RememberMe"].Value   = objLogin.rememberMe.ToString();
                }
                else
                {
                    Response.Cookies["rem_user"].Expires   = DateTime.Now.AddDays(-1);
                    Response.Cookies["RememberMe"].Expires = DateTime.Now.AddDays(-1);
                }


                if (ModelState.IsValid)
                {
                    LoginModels obj = new LoginModels();


                    obj = obj.getLoginInfo(objLogin);

                    if (obj != null)
                    {
                        LoginSessionDetails objLogSession = new LoginSessionDetails();
                        objLogSession.objLoginM       = obj;
                        Session["SessionInformation"] = objLogSession;
                        if (obj.IsAdmin)
                        {
                            return(RedirectToAction("Admin", "Account", new { returnUrl = (this.HttpContext.Request).Path }));
                        }
                        else
                        {
                            return(RedirectToAction("Index", "Account", new { returnUrl = (this.HttpContext.Request).Path }));
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("ErrorMgr", "Invalid Agent or Password!");
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(View(objLogin));
        }
        public ActionResult Login(LoginModels objLogin)
        {
            bool vLicenseExpired = false;

            string returnUrl = "";

            Response.Cookies["RememberMe"].Value = objLogin.RememberMe.ToString();
            if (objLogin.RememberMe)
            {
                var userencrypt = string.Join(",", objLogin.UserName, objLogin.password, objLogin.RememberMe);
                Response.Cookies["rem_user"].Expires = DateTime.Now.AddDays(30);
                Response.Cookies["rem_user"].Value   = objLogin.Encryptdata(userencrypt);

                Response.Cookies["RememberMe"].Expires = DateTime.Now.AddDays(30);
                Response.Cookies["RememberMe"].Value   = objLogin.RememberMe.ToString();
            }
            else
            {
                Response.Cookies["rem_user"].Expires   = DateTime.Now.AddDays(-1);
                Response.Cookies["RememberMe"].Expires = DateTime.Now.AddDays(-1);
            }


            try
            {
                //Validating user for required fields
                if (ModelState.IsValid)
                {
                    string[] user;
                    if (Request.LogonUserIdentity.Name.Contains("\\"))
                    {
                        user = Request.LogonUserIdentity.Name.Replace("\\\\", "\\").Split('\\');
                    }
                    else
                    {
                        user = new string[2] {
                            "", Request.LogonUserIdentity.Name.Replace("\\\\", "\\").ToString()
                        };
                    }

                    string username   = user[1];
                    string mc         = Environment.MachineName;
                    string domainName = user[0];
                    string bTyp       = Request.Browser.Type;
                    string bVer       = Request.Browser.Version;
                    /*GET IP*/
                    string localIP = "";

                    bool     ISDomain     = false;
                    string   emailaddress = "";
                    string[] aduname      = objLogin.UserName.Split('\\');
                    if (aduname.Length > 1)
                    {
                        var userdata = objLogin.ADIsValid(aduname[0], aduname[1], objLogin.password);
                        ISDomain     = userdata.Item1;
                        emailaddress = userdata.Item2;
                    }

                    string strErrorMsg = "";
                    LoginSessionDetails objLogSession = objLog.getLoginInfo(objLogin);

                    //string vEncryptString = "";
                    //vEncryptString = CommonMethods.Encrypt("30 Jul 2016 ", "ERPWEB");


                    //-------------------------------------------------------------------
                    // License Validation
                    //-------------------------------------------------------------------
                    //if (objLogSession.USERID != 0)
                    //{
                    //    string vDecryptString = "";
                    //    vDecryptString = CommonMethods.Decrypt(objLogSession.SVRKEY, "ERPWEB");

                    //    if (vDecryptString.Trim() == "")
                    //    {
                    //        objLogSession.USERID = 0;
                    //        vLicenseExpired = true;
                    //    }
                    //    else if (Convert.ToDateTime(vDecryptString) < objLogSession.SVRDATE)
                    //    {
                    //        objLogSession.USERID = 0;
                    //        vLicenseExpired = true;
                    //    }
                    //}
                    //-------------------------------------------------------------------


                    //Form setializing user object and encrypting
                    string userData = JsonConvert.SerializeObject(objLogSession);
                    FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, username, DateTime.Now, DateTime.Now.AddDays(1), false, userData);
                    string     encTicket = FormsAuthentication.Encrypt(authTicket);
                    HttpCookie faCookie  = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
                    //faCookie.Expires = authTicket.Expiration; // comment for use as non persistence cookie
                    Response.Cookies.Add(faCookie);

                    if (objLogSession != null && objLogSession.USERID != 0)
                    {
                        Session["SessionInformation"] = objLogSession;
                        string time = DateTime.Now.AddMinutes(1).ToString("mm.ss");
                        Session["ReminderTime"] = time;
                        if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                        {
                            return(Redirect(returnUrl));
                        }
                        else
                        {
                            if (System.Configuration.ConfigurationManager.AppSettings["DefaultUrl"] == "")
                            {
                                return(RedirectToAction("CompanyDtl", "Company", new { returnUrl = (this.HttpContext.Request).Path }));
                            }
                            else
                            {
                                return(Redirect(System.Configuration.ConfigurationManager.AppSettings["DefaultUrl"]));
                            }
                        }
                    }
                    else
                    {
                        if (vLicenseExpired)
                        {
                            ModelState.AddModelError("ErrorMgr", "License expired. Please renew your License.");
                        }
                        else
                        {
                            ModelState.AddModelError("ErrorMgr", "Invalid login id or password!"); // "The user name or password provided is incorrect."
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw;
            }
            return(View(objLogin));
        }