Reputation: 141
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.
Upvotes: 4
Views: 6185
Reputation: 1
The FormsAuthentication.SetAuthCookie
method is part of the System.Web.Security
namespace in ASP.NET, which is included in the .NET Framework. Therefore, you can use the FormsAuthentication.SetAuthCookie
method in your ASP.NET application without any additional packages or libraries.
Have you tried this : FormsAuthentication.SetAuthCookie doesn't [Authorize] in MVC 5
Upvotes: -1
Reputation: 3371
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
Upvotes: 10