come aggiornare il security stamp
quando l'utente si disconnette?
è possibile aggiornare il security stamp
quando l'utente si disconnette?
il mio codice:
public class SignInManager : SignInManager<User>, ISignInManager
{
// other
public override async Task SignOutAsync()
{
await _userManager.UpdateSecurityStampCurrentUserAsync();
await base.SignOutAsync();
}
}
public async Task<IdentityResult> UpdateSecurityStampCurrentUserAsync()
{
return await UpdateSecurityStampAsync(GetCurrentUser());//error
}
public User GetCurrentUser()
{
if (_httpContextAccessor.HttpContext.User == null)
{
return null;
}
var userId = Guid.Parse(_httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value);
return currentUser ?? (currentUser = _users.First(d => d.Id == userId));
}
errore:
L'istanza del tipo di entità "Utente" non può essere tracciata perché un'altra traccia di questo tipo con la stessa chiave è già stata tracciata. Per le nuove entità considerare l'utilizzo di un IIdentityGenerator per generare valori chiave univoci.
Configura servizi:
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddDbContext<DotNetContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DotNetConnection")));
services.AddIdentity<User, Role>()
.AddEntityFrameworkStores<DotNetContext, Guid>()
.AddUserManager<UserManager>()
.AddRoleManager<RoleManager>()
.AddUserStore<UserStore>()
.AddDefaultTokenProviders();
services.AddMvc();
// Services
services.AddScoped<IUserManager, UserManager>();
services.AddScoped<IRoleManager, RoleManager>();
services.AddScoped<ISignInManager, SignInManager>();
services.AddScoped<IUserStore, UserStore>();
}
Usa questo codice:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> LogOff()
{
var user = await _userManager.FindByNameAsync(User.Identity.Name);
_authenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
await _userManager.UpdateSecurityStampAsync(user.Id);
return RedirectToAction("Index", "Home");
}