Exemplo n.º 1
0
        public AdminUserContext(IKernel kernel)
        {
            if (Thread.CurrentPrincipal == null || Thread.CurrentPrincipal.Identity == null || !Thread.CurrentPrincipal.Identity.IsAuthenticated)
            {
                throw new Exception("Anonymous user access");
            }
            else
            {
                var claimsIdentity = Thread.CurrentPrincipal.Identity as ClaimsIdentity;

                List <Claim> roleClaims = claimsIdentity.Claims
                                          .Where(c => c.Type == ClaimTypes.Role)
                                          .Select(c => c).ToList();

                GSLogisticsUserContext user;


                var userName = Microsoft.AspNet.Identity.IdentityExtensions.GetUserName(Thread.CurrentPrincipal.Identity);
                if (!string.IsNullOrWhiteSpace(userName))
                {
                    user = new GSLogisticsUserContext(kernel, userName);
                }
                else
                {
                    throw new InvalidOperationException("Invalid Identity");
                }



                _UserContext = user;
            }
        }
Exemplo n.º 2
0
        public async Task <ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                ApplicationUser user = await UserManager.FindAsync(model.UserName, model.Password);

                if (user == null)
                {
                    ModelState.AddModelError("", "Invalid user name or password.");
                }
                else
                {
                    ClaimsIdentity identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);

                    AuthManager.SignOut();
                    AuthManager.SignIn(new AuthenticationProperties
                    {
                        IsPersistent = false
                    }, identity);


                    // clientTest / test1
                    var userContext = new GSLogisticsUserContext(_kernel, user.UserName);
                    Session["UserContext"] = userContext;
                    ViewBag.UserName       = userContext.UserName;

                    if (userContext.CustomerIds.Any())
                    {
                        return(Redirect(string.IsNullOrEmpty(returnUrl) ? "/OrderAppointment/LogReport" : returnUrl));
                    }
                    return(Redirect(string.IsNullOrEmpty(returnUrl) ? "/OrderAppointment/List" : returnUrl));
                }
                //if (authProvider.Authenticate(model.UserName, model.Password))
                //{
                //    return Redirect(returnUrl ?? Url.Action("List", "OrderAppointment"));
                //}
                //else
                //{
                //    ModelState.AddModelError("", "Incorrect user name or password");
                //    return View();
                //}
            }

            ViewBag.ReturnUrl = returnUrl;
            return(View(model));
        }