I need the ability to authentificate my user from code in test like this
var identity = new GenericIdentity("[email protected]", "UserId");
Thread.CurrentPrincipal = new GenericPrincipal(identity, null);
Or like this
FormsAuthentication.SetAuthCookie(username, rememberMe);
But these methods not working in asp net 5.
To get this to work in ASP.NET Core, first, use NuGet to add Microsoft.AspNetCore.Authentication.Cookies
In Startup.cs, add this to the Configure method:
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
AuthenticationScheme = "PutANameHere",
LoginPath = new PathString("/Account/Login/"),
AutomaticAuthenticate = true,
AutomaticChallenge = true
});
Then call this to login:
var userClaims = new List<Claim>
{
new Claim(ClaimTypes.Name, userName)
};
var principal = new ClaimsPrincipal(new ClaimsIdentity(userClaims, "local"));
HttpContext.Authentication.SignInAsync("PutANameHere", principal);
For more information on this, check this out: https://docs.asp.net/en/latest/security/authentication/cookie.html
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