если вам нужно что-то отличное от аутентификации по умолчанию , вы можете использовать что-то вроде этого
сначала создайте простой пользовательский класс
public class MyCustomUser
{
public int Id { get; set; }
public string Name { get; set; }
public string GivenName { get; set; }
}
в startup.cs внутри ConfigureServices
метод
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.ExpireTimeSpan = TimeSpan.FromDays(7);
options.LoginPath = "/Account/CustomLogin";
options.Cookie.Name = "MyAuthCookieName";
}
);
в startup.cs внутри Configure
метод
app.UseAuthentication();
затемна вашем SignIn
действии в вашем контроллере вы могли бы написать что-то вроде этого, что бы сохранить информацию пользователя в претензиях ( что такое претензии? )
//Inside your SignIn method
//User info should be taken from DB
MyCustomUser user = new MyCustomUser()
{
Id = 1,
Name = "Mr.Awesome",
GivenName = "John Doe"
};
//Add user information
List<Claim> claims = new List<Claim>
{
new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()),
new Claim(ClaimTypes.Name, user.Name),
new Claim(ClaimTypes.GivenName, user.GivenName)
};
//Create the principal user from the claims
ClaimsIdentity identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
ClaimsPrincipal principal = new ClaimsPrincipal(identity);
AuthenticationProperties authenticationProperties = new AuthenticationProperties() {IsPersistent = false};
//Create the authentication cookie and store it
await this.HttpContext
.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
principal, authenticationProperties);
// DONE!