相关疑难解决方法(0)

如何更新ASP.NET标识中的声明?

我正在为我的MVC5项目使用OWIN身份验证.这是我的SignInAsync

 private async Task SignInAsync(ApplicationUser user, bool isPersistent)
        {
            var AccountNo = "101";
            AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
            var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
            identity.AddClaim(new Claim(ClaimTypes.UserData, AccountNo));
            AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent, RedirectUri="Account/Index"}, identity);
        }
Run Code Online (Sandbox Code Playgroud)

如您所见,我添加AccountNo到声明列表中.

现在,如何在我的应用程序中的某个时刻更新此声明?到目前为止,我有这个:

 public string AccountNo
        {

            get
            {
                var CP = ClaimsPrincipal.Current.Identities.First();
                var Account= CP.Claims.FirstOrDefault(p => p.Type == ClaimTypes.UserData);
                return Account.Value;
            }
            set
            {
                var CP = ClaimsPrincipal.Current.Identities.First();
                var AccountNo= CP.Claims.FirstOrDefault(p => p.Type == ClaimTypes.UserData).Value;
                CP.RemoveClaim(new Claim(ClaimTypes.UserData,AccountNo));
                CP.AddClaim(new Claim(ClaimTypes.UserData, value));
            }

        }
Run Code Online (Sandbox Code Playgroud)

当我尝试删除声明时,我得到以下异常:

声明' http://schemas.microsoft.com/ws/2008/06/identity/claims/userdata:101 …

c# asp.net asp.net-mvc asp.net-mvc-4 asp.net-web-api

79
推荐指数
6
解决办法
8万
查看次数

表单身份验证 - Cookie重放攻击 - 保护

我被问及有关我的ASP.NET网站表单身份验证的cookie重放攻击.

我遵循了下面的建议,以防止任何攻击,但认为如果有人设法获取cookie(尽管只是很短的时间),该网站仍然是脆弱的.有没有办法在注销时完全销毁表单身份验证会话,这样即使有人偷了cookie,也没有机会恶意使用它

随后的建议是

我们相信,我们已经采取了所有可靠的步骤,以便在ASP.NET的范围内防止这种情况发生.请参阅下面的详细回复.

但是,我们已经实施了Microsoft建议的步骤来防范这种情况(请参阅http://support.microsoft.com/default.aspx?scid=kb;en-us;900111)

·身份验证cookie永远不会写入客户端计算机,因此很难窃取.

·应用程序可通过SSL运行,因此cookie永远不会通过非安全连接发出

·我们使用15分钟的超时强制执行绝对到期,这意味着在该时间限制之后任何问题cookie都无用

·我们使用httpOnly cookie,以便没有人可以通过编程方式拦截或更改此cookie.

因此,即使上述预防措施被打破,我们认为这种预防措施极不可能,恶意用户只需要15分钟的时间来打破预防措施并成功登录

asp.net security

7
推荐指数
1
解决办法
7493
查看次数