shreesharao / aspnetcore-ConsoleToWeb

Convert Console project to web project for understanding the working of asp.net core mvc
0 stars 0 forks source link

How - Using Cookie Authentication without AspNetCore Identity #14

Closed shreesharao closed 6 years ago

shreesharao commented 6 years ago

In this issue - https://github.com/shreesharao/aspnetcore-ConsoleToWeb/issues/13, I have checked how to use identity for authentication. Now i am checking Cookie authentication without identity.

shreesharao commented 6 years ago

Use CookieAuthenticationDefaults.AuthenticationScheme as the AuthenticationScheme.

[HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Login(LoginViewModel loginViewModel)
        {

            if (ModelState.IsValid)
            {
                //var result = await _signInManager.PasswordSignInAsync(loginViewModel.Email, loginViewModel.Password, loginViewModel.Rememberme, lockoutOnFailure: true);

                //if (result.Succeeded)
                //{

                    //set cookie
                    var claims = new List<Claim>()
                    {
                        new Claim(ClaimTypes.Name,loginViewModel.Email),
                        new Claim(ClaimTypes.Role,$"Administrator")
                    };

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

                    var authProperties = new AuthenticationProperties();

                    //Make the cookie persisitent if the user wants to
                    if(loginViewModel.Rememberme)
                    {
                        authProperties.IsPersistent = true;
                    }

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

                    return RedirectToAction("Dashboard", "Library");
                //}
                //else
                //{
                //    _logger.LogError($"{result}");
                //}
            }

            return View(loginViewModel);
        }

LibraryController.cs

public class LibraryController : Controller
    {
        [HttpGet]
        [Authorize(AuthenticationSchemes = CookieAuthenticationDefaults.AuthenticationScheme , Roles = "Administrator")]
        public IActionResult Dashboard()
        {
            return View();
        }
    }

Dashboard.cshtml Use User.Identity.IsAuthenticated to check authenticated user.

@if (User.Identity.IsAuthenticated)//SignInManager.IsSignedIn(User)
{
    <h3>upon successfull authentication we will reach here</h3>
    <ul>
        @foreach (Claim claim in User.Claims)
        {
            <li>@claim.Subject.Name</li>
            <li>@claim.Value</li>
        }
    </ul>

}
else
{
    <ul>
        <li>
            <a asp-action="Login" asp-controller="Profile">Log In</a>
        </li>
        <li>
            <a asp-action="Register" asp-controller="Profile">Register</a>
        </li>
    </ul>
}