我正在使用ASP.NET MVC 5.1与Owin和声明身份验证.
用户更改其电子邮件后,我需要更新用户声明,所以我尝试在控制器中:
ClaimsIdentity identity = (ClaimsIdentity)User.Identity;
Claim claim = identity.FindFirst(ClaimTypes.Email);
identity.RemoveClaim(claim);
identity.AddClaim(new Claim(ClaimTypes.Email, newEmail));
IOwinContext context = new OwinContext();
context.Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie);
context.Authentication.SignIn(identity);
Run Code Online (Sandbox Code Playgroud)
索赔已更改,但当我刷新页面时,电子邮件声明再次原始...
似乎cookie没有更新.知道我做错了什么吗?
是否有可能从身份中获取"IsPersistent"的值,所以当我再次签名时,我将获得相同的值?
谢谢,
米格尔
我发现如果我将用户添加到ASP身份中的角色,它会在我退出并重新登录之后才会生效.我是否需要做一些事情来刷新用户的角色而不强迫用户登录先关掉?
以下是我将用户添加到角色的方式.
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
var userId = HttpContext.Current.User.Identity.GetUserId();
userManager.AddToRole(userId, roleName);
Run Code Online (Sandbox Code Playgroud)
然后,几乎立即,我将用户重定向到此操作方法.我可以在数据库中告诉我已经添加到正确的角色,但MVC仍然将我重定向到登录页面.但是,如果我退出,重新登录,并尝试转到此操作方法,它可以正常工作.
[Authorize(Roles = RecoveryStandardRoles.ServiceProvider)]
public partial class CertifyController : Controller
{
#region Public Methods and Operators
public virtual ActionResult CompanyProfile()
{
return this.View();
}
#endregion
}
Run Code Online (Sandbox Code Playgroud)
感谢您花时间看我的问题!
那么什么是ASP.NET Identity的IUserSecurityStampStore <TUser>接口?我们了解到ASP.NET Identity具有安全标记功能,用于使用户登录cookie无效,并强制他们重新登录.
在我的MVC应用程序中,管理员可以归档用户.当拱形时,它们应该立即被注销并被迫再次登录(这将因为它们被存档而拒绝它们).
我怎样才能做到这一点?我知道安全标记是关键.默认设置如下所示:
app.UseCookieAuthentication(new CookieAuthenticationOptions {
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider {
// Enables the application to validate the security stamp when the user logs in.
// This is a security feature which is used when you change a password or add an external login to your account.
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});
Run Code Online (Sandbox Code Playgroud)
通过实验,如果我将validateInterval设置为1分钟,然后在数据库中手动破解用户安全标记,那么他们将被迫重新登录,但仅在该时间段过去之后.
有没有办法让这个瞬间,或者只是将间隔设置为一个低时间段并等待(或实现我自己的OnValidateIdentity检查每个请求)
谢谢
asp.net authentication asp.net-mvc asp.net-mvc-5 asp.net-identity
我将用户选择的文化存储到用户声明中,并且我有一个自定义 RequestCultureProvider 读取此值并相应地设置请求文化。
该应用程序将有一个个人资料页面,用户可以在其中更改他的偏好(包括文化)。将数据保存到数据库后,我需要默默地重新登录用户以更新他的声明。
附加信息:
那么,如何使用 Idsrv4 实现静默重新签名?
authentication asp.net-identity-3 identityserver4 asp.net-core-2.0