Esempio n. 1
0
        public IActionResult Upsert(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                ViewBag.Roles = _pr.GetRoles();
                return(View(new PortalLoginViewModel()));
            }

            var dto = _pr.GetPortalLogin(id);

            if (dto == null)
            {
                return(NotFound());
            }

            PortalLoginViewModel viewModel = _mapper.Map <PortalLoginViewModel>(dto);

            ViewBag.Roles = _pr.GetRoles();
            return(View("Upsert", viewModel));
        }
Esempio n. 2
0
        public async Task <IActionResult> Login(LoginViewModel model, string returnUrl)
        {
            ViewBag.ReturnUrl = returnUrl;

            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            //var combination = model.Username.Split("@");
            //var username = combination[0];
            //if (combination.Length == 1)
            //{
            //    if (username == "jingteng")
            //    {
            //        // it's correct, superadmin account
            //    }
            //    else
            //    {
            //        ModelState.AddModelError("Username", "用户名错误。");
            //        return View(model);
            //    }
            //}
            //else
            //{
            //    if (combination[1].ToLower() != "jingteng")
            //    {
            //        ModelState.AddModelError("Username", "用户名错误。");
            //        return View(model);
            //    }
            //}

            var user = _pr.VerifyPortalLogin(model.Username, model.Password);

            if (user == null)
            {
                ModelState.AddModelError("Password", "用户名或密码错误。");
                return(View(model));
            }

            var roles  = _pr.GetPortalLogin(user.Id).Roles;
            var claims = new List <Claim>
            {
                new Claim(ClaimTypes.Name, user.Username),
                new Claim(ClaimTypes.NameIdentifier, user.Id),
                new Claim(ClaimTypes.Role, roles)
            };

            var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);

            var authProperties = new AuthenticationProperties
            {
                //AllowRefresh = <bool>,
                // Refreshing the authentication session should be allowed.

                //ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(10),
                // The time at which the authentication ticket expires. A
                // value set here overrides the ExpireTimeSpan option of
                // CookieAuthenticationOptions set with AddCookie.

                IsPersistent = true,
                // Whether the authentication session is persisted across
                // multiple requests. Required when setting the
                // ExpireTimeSpan option of CookieAuthenticationOptions
                // set with AddCookie. Also required when setting
                // ExpiresUtc.

                //IssuedUtc = <DateTimeOffset>,
                // The time at which the authentication ticket was issued.

                //RedirectUri = <string>
                // The full path or absolute URI to be used as an http
                // redirect response value.
            };

            await HttpContext.SignInAsync(
                CookieAuthenticationDefaults.AuthenticationScheme,
                new ClaimsPrincipal(claimsIdentity),
                authProperties);

            if (user.IsPasswordToChange)
            {
                return(RedirectToRoute(new { Controller = "Account", Action = "ChangePassword" }));
            }

            return(RedirectToRoute(new { Controller = "Home", Action = "Index" }));
        }