Sknecht
Sknecht

Reputation: 1074

How to set a Auth cookie in asp.net 5

Trying to make a very simple form of authentication in asp.net5 but seems like they removed the form authentication. What else could I use to make a very simple authentication?

Thanks in advance

Upvotes: 2

Views: 4452

Answers (1)

Anuraj
Anuraj

Reputation: 19608

You can do something like this.

if (authUser.Username == "admin" && authUser.Password == "admin")
    {
        var claims = new[] { new Claim("name", authUser.Username), new Claim(ClaimTypes.Role, "Admin") };
        var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
        Context.Authentication.SignIn(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity));

        return Redirect("~/Home/Index");
    }

Sample code - https://github.com/anuraj/CookieAuthMVCSample

Upvotes: 6

Related Questions