コード例 #1
0
        public async Task <IActionResult> Login(LoginModel loginModel)
        {
            if (!adHelper.Authenticate(loginModel.UserName, loginModel.Password))
            {
                return(View());
            }



            string role = "User";

            if (adHelper.isAdmin(loginModel.UserName))
            {
                role = "Administrator";
            }
            else if (adHelper.IsHR(loginModel.UserName))
            {
                role = "HR";
            }
            // create claims
            List <Claim> claims = new List <Claim>
            {
                new Claim(ClaimTypes.Name, loginModel.UserName),
                new Claim(ClaimTypes.Role, role)
            };

            // create identity
            ClaimsIdentity identity = new ClaimsIdentity(claims, "cookie");

            // create principal
            ClaimsPrincipal principal = new ClaimsPrincipal(identity);

            // sign-in
            await HttpContext.SignInAsync(
                scheme : "SecurityCookie",
                principal : principal);

            return(RedirectToAction("Index", new RouteValueDictionary(
                                        new { controller = "Home", action = "Index", loginModel.UserName })));
        }