Exemplo n.º 1
0
        public static void Initialize(ApplicationDbContext context)
        {
            context.Database.EnsureCreated();

            if (context.IdentityUser.Any())
            {
                return;
            }

            var salt = Guid.NewGuid().ToString();

            IdentityUser admin = new IdentityUser
            {
                Name         = "管理员",
                Account      = "admin",
                AccountType  = 0,
                Age          = 0,
                Birthday     = new DateTime(),
                Salt         = salt,
                Password     = Md5Utility.Sign("123456", salt),
                Gender       = true,
                IsEnabled    = true,
                Email        = "*****@*****.**",
                HomePage     = "Administrator",
                IdNumber     = "0",
                DepartmentId = 0,
                Department   = "管理员部门"
            };

            context.IdentityUser.Add(admin);
            context.SaveChanges();
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Login(LoginViewModel viewModel, string returnUrl)
        {
            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {
                var user = await _service.GetUserAsync(viewModel.Account, viewModel.Password, _context);

                if (user != null)
                {
                    if (user.Password.Trim() != Md5Utility.Sign(viewModel.Password, user.Salt))
                    {
                        ViewBag.ErrorInfo = "用户名或密码错误";
                        return(View());
                    }

                    _logger.LogInformation("用户:{0}于{1}登录系统", viewModel.Account, DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));

                    string role = GetRole(user);

                    //根据用户角色创建claim声明
                    List <Claim> claim = new List <Claim>
                    {
                        new Claim(ClaimTypes.Role, role)
                    };

                    var userIdentity = new ClaimsIdentity(role);
                    userIdentity.AddClaims(claim);

                    var userPrincipal = new ClaimsPrincipal(userIdentity);

                    await HttpContext.SignInAsync(userPrincipal, new AuthenticationProperties
                    {
                        ExpiresUtc   = DateTime.UtcNow.AddMinutes(20),
                        IsPersistent = false,
                        AllowRefresh = false
                    });

                    //设置当前用户信息
                    await _service.SetCurrentUser(user.IdentityUserOID, _httpContextAccessor, _context);

                    if (!string.IsNullOrEmpty(returnUrl))
                    {
                        return(RedirectToLocal(returnUrl));
                    }

                    return(RedirectToRoute(new
                    {
                        area = user.HomePage,
                        controller = "Home",
                        action = "Index"
                    }));
                }
                else
                {
                    ViewBag.ErrorInfo = "当前用户不存在";
                    return(View());
                }
            }

            //返回模型验证错误信息
            ViewBag.ErrorInfo = this.ModelState.Keys.SelectMany(key => this.ModelState[key].Errors).FirstOrDefault()?.ErrorMessage;
            return(View(viewModel));
        }