コード例 #1
0
ファイル: WalkersController.cs プロジェクト: j0verton/DogGo
        public async Task <ActionResult> Login(LoginViewModel viewModel)
        {
            Walker walker = _walkerRepo.GetWalkerByEmail(viewModel.Email);

            if (walker == null)
            {
                return(Unauthorized());
            }

            List <Claim> claims = new List <Claim>
            {
                new Claim(ClaimTypes.NameIdentifier, walker.Id.ToString()),
                new Claim(ClaimTypes.Email, walker.Email),
                new Claim(ClaimTypes.Role, "Walker"),
            };

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

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

            return(RedirectToAction("Index", "Walks"));
        }
コード例 #2
0
        public async Task <ActionResult> Login(LoginViewModel viewModel)
        {
            List <Claim> claims = new List <Claim>();

            if (viewModel.Role == "Owner")
            {
                Owner owner = _ownerRepo.GetOwnerByEmail(viewModel.Email);

                if (owner == null)
                {
                    return(Unauthorized());
                }

                claims = new List <Claim>
                {
                    new Claim(ClaimTypes.NameIdentifier, owner.Id.ToString()),
                    new Claim(ClaimTypes.Email, owner.Email),
                    new Claim(ClaimTypes.Role, "DogOwner"),
                };

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

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

                return(RedirectToAction("Details", "Owners", new { id = owner.Id }));
            }
            else if (viewModel.Role == "Walker")
            {
                Walker walker = _walkerRepo.GetWalkerByEmail(viewModel.Email);

                if (walker == null)
                {
                    return(Unauthorized());
                }

                claims = new List <Claim>
                {
                    new Claim(ClaimTypes.NameIdentifier, walker.Id.ToString()),
                    new Claim(ClaimTypes.Email, walker.Email),
                    new Claim(ClaimTypes.Role, "DogWalker"),
                };

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

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

                return(RedirectToAction("Details", "Walkers", new { id = walker.Id }));
            }

            return(NotFound());
        }