相关疑难解决方法(0)

更新用户声明未生效.为什么?

我正在使用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.net-mvc asp.net-identity

18
推荐指数
2
解决办法
2万
查看次数

MVC 5 AddToRole在运行之前需要注销吗?

我发现如果我将用户添加到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-mvc-5 asp.net-identity

11
推荐指数
3
解决办法
7717
查看次数

ASP.NET标识 - 强制使用安全戳重新登录

那么什么是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

10
推荐指数
1
解决办法
7312
查看次数

刷新登录和重新加载声明

我将用户选择的文化存储到用户声明中,并且我有一个自定义 RequestCultureProvider 读取此值并相应地设置请求文化。

该应用程序将有一个个人资料页面,用户可以在其中更改他的偏好(包括文化)。将数据保存到数据库后,我需要默默地重新登录用户以更新他的声明。

附加信息:

  • 我将 IdentityServer4 与 AspNet Core 2.0 和 Asp.Net Identity 一起使用
  • 我正在将文化加载到 OnTokenValidated 事件(客户端应用程序)中的声明中。也可以在 GetProfileDataAsync (IProfileService) 或 UserClaimsPrincipalFactory (ASP.NET Core Identity) 中完成
  • 该系统由 3 个 Web 应用程序(Idsv4 + app1 + app2)组成。个人资料页面在应用程序中实现。
  • 使用配置了 Asp.NET Identity 的单个 Web 应用程序,您可以使用 SignInManager 中的 RefreshSignInAsync 方法重新生成用户的应用程序 cookie,但是我需要从客户端应用程序(app1 和 app2)触发此过程,因此无法访问 SignInManager。
  • 我尝试使用 HttpContext.Authentication.ChallengeAsync("oidc") HttpContext.Authentication.SignInAsync 并且显然调用了身份验证端点,但是我无法处理响应并且它在我调用此代码的 MVC 操作中生成无限循环。

那么,如何使用 Idsrv4 实现静默重新签名?

authentication asp.net-identity-3 identityserver4 asp.net-core-2.0

5
推荐指数
0
解决办法
878
查看次数