We're adding cookie authentication to a fresh ASP.NET 5 application. (currently using Beta 7)
We added this to Startup.cs:
//Use cookie authentication
app.UseCookieAuthentication(options => {
options.LoginPath = new PathString("/Login");
options.AutomaticAuthentication = true;
});
And the [Authorize]
attribute is working fine, redirecting to the login page.
Inside our Login() controller method, we're creating claims and an identity:
var claims = new List<Claim>();
claims.Add(new Claim("name", loginRequest.Username));
var identity = new ClaimsIdentity(claims.ToArray(),
CookieAuthenticationDefaults.AuthenticationScheme);
As the last step, we're trying to call the SignIn() method.
Context.Response.SignIn(identity); //what is the namespace for SignIn()??
But we can't find the namespace for the SignIn() method. Our goal is use the simplest possible solution without the full Identity framework. (although I'm okay with referencing it if necessary to get the SignIn() plumbing to work.)
What is the namespace for the SignIn() method mentioned in the documentation?
I figured it out - looks like I was using some out of date documentation. I should have been calling SignIn() like this:
await Context.Authentication.SignInAsync(
CookieAuthenticationDefaults.AuthenticationScheme,
new ClaimsPrincipal(identity));
This article and the simplified example he created was incredibly helpful. Thanks!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With