Esempio n. 1
0
        public virtual ActionResult LogOn(LogOnModel model, string returnUrl)
        {

            try
            {
                
                if (ModelState.IsValid)
                {
                    //string returnUrl = model.ReturnUrl;
                    string userid = HttpUtility.UrlDecode(model.UserID);
                    //string Domain = ConfigurationManager.AppSettings["Domain"];
                    //bool isValidUser = this._repo.ValidateCredentials(userid, model.Password, Domain);
                    if (Membership.ValidateUser(userid, model.Password))
                    {
                        string ApplicationKey = ConfigurationManager.AppSettings["ApplicationKey"];
                        Users us = new UsersRepository().GetUserDetailFromDatabase(userid);
                        Token token = new Token(userid, ControllerContext.HttpContext.Request.UserHostAddress, ApplicationKey, us.AccessFeatures);
                        string res = token.EncryptKey();
                        if (!string.IsNullOrWhiteSpace(res))
                        {
                            Response.Cookies.Add(new HttpCookie("AuthToken", HttpUtility.UrlEncode(res)) { Expires = DateTime.Now.AddDays(1) });
                            Response.Cookies.Add(new HttpCookie("UserID", userid) { Expires = DateTime.Now.AddDays(1) });

                            MasterRepository rep = new MasterRepository();
                            Company com = rep.GetCompanyInfo(1);// temp company id
                            
                            string userInfo = JsonConvert.SerializeObject(new UserInfo() 
                                                                            { 
                                                                                UserID = userid,
                                                                                CompanyID = com.CompanyID,
                                                                                CompanyName = com.Name,
                                                                                Name = us.Name, 
                                                                                Title = us.Title,
                                                                                Email = us.Email
                                                                            });
                            
                            Response.Cookies.Add(new HttpCookie("UserInfo", userInfo) { Expires = DateTime.Now.AddDays(1) });
                            
                            FormsAuthentication.SetAuthCookie(model.UserID, model.RememberMe);
                            string decodedUrl = "";
                            if (!string.IsNullOrEmpty(returnUrl))  decodedUrl = Server.UrlDecode(returnUrl);
                            if (Url.IsLocalUrl(decodedUrl))
                            {
                                return Redirect(decodedUrl.Replace("/home","/#"));
                            }
                            else
                            {
                                return RedirectToAction("Index", "Home");
                            }
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("", "Invalid credentials. Please try again!");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Invalid input. Please enter correct fields and try again!");
                }

            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
            }
            // If we got this far, something failed, redisplay form
            return PartialView(model);
        }