Example #1
0
        public RedirectToActionResult Login(string username, string password)
        {
            try
            {
                string browserInfo = "Unable to determine"; // defaults
                string deviceInfo  = "Unable to determine"; // defaults

                if (!Convert.ToBoolean(Configuration["MockData.Enabled"]))
                {
                    try
                    {
                        UserAgentHelper.SetUserAgent(Request.Headers["User-Agent"]);
                        browserInfo = UserAgentHelper.Browser.Name + " " + UserAgentHelper.Browser.Version + " " + UserAgentHelper.Browser.Major;
                        deviceInfo  = UserAgentHelper.OS.Name + " " + UserAgentHelper.OS.Version;
                    }
                    catch { }
                }

                var userModel = WebApiCaller.PostAsync <UserModel>("WebApi:Authenticate:Login", new LoginRequestModel
                {
                    Username = username,
                    Password = password,
                    Browser  = browserInfo,
                    Device   = deviceInfo
                });

                if (userModel.Username != null)
                {
                    if (userModel.IsAuthenticated)
                    {
                        CookieHelper.SignIn(userModel);
                        SecurityHelper.AddSessionUser(userModel);
                        return(RedirectToAction("Index", "Orders"));
                    }
                    else
                    {
                        return(RedirectToAction("Index", "Authenticate", new AuthenticateViewModel {
                            Username = username, ErrorMessage = "Invalid password. Please try again."
                        }));
                    }
                }
                else
                {
                    return(RedirectToAction("Index", "Authenticate", new AuthenticateViewModel {
                        ErrorMessage = "Username does not exist."
                    }));
                }
            }
            catch (Exception ex)
            {
                return(RedirectToAction("Error", "Home", new { IsError = "True", ex.Message, BaseMessage = ex.GetBaseException().Message }));
            }
        }
Example #2
0
        public RedirectToActionResult Login(string username, string password)
        {
            try
            {
                string browserInfo = "Unable to determine";
                string deviceInfo  = "Unable to determine";

                try
                {
                    UserAgentHelper.SetUserAgent(Request.Headers["User-Agent"]);
                    browserInfo = UserAgentHelper.Browser.Name + " " + UserAgentHelper.Browser.Version + " " + UserAgentHelper.Browser.Major;
                    deviceInfo  = UserAgentHelper.OS.Name + " " + UserAgentHelper.OS.Version;
                }
                catch {  }

                var userModel = WebApiCaller.PostAsync <UserModel>("WebApi:Authenticate:Login", new LoginRequestModel
                {
                    Username = username,
                    Password = password,
                    Browser  = browserInfo,
                    Device   = deviceInfo
                });

                if (userModel.Username != null)
                {
                    if (userModel.IsAuthenticated)
                    {
                        CookieHelper.SignIn(userModel);
                        CookieHelper.SetCookie("LoggedInUser", JsonConvert.SerializeObject(userModel)); // put the encrypted version of the api session token
                        return(RedirectToAction("Index", "Orders", new { area = "Orders", userModel.Username }));
                    }
                    else
                    {
                        return(RedirectToAction("Index", "Authenticate", new AuthenticateViewModel {
                            Username = username, ErrorMessage = "Invalid password. Please try again."
                        }));
                    }
                }
                else
                {
                    return(RedirectToAction("Index", "Authenticate", new AuthenticateViewModel {
                        ErrorMessage = "Username does not exist."
                    }));
                }
            }
            catch (Exception ex)
            {
                return(RedirectToAction("Error", "Home", new { area = "Home", IsError = "True", ex.Message, BaseMessage = ex.GetBaseException().Message }));
            }
        }