基于我的理解,OAuth 2.0流中的state
参数旨在用作防止CSRF的防伪攻击令牌。
这是Asp.Net
身份模板的默认实现
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LinkLogin(string provider)
{
// Request a redirect to the external login provider to link a login for the current user
return new AccountController.ChallengeResult(provider, Url.Action("LinkLoginCallback", "Manage"), User.Identity.GetUserId());
}
在AccountController.ChallengeResult.ExecuteResult
中,这就是我们的
public override void ExecuteResult(ControllerContext context)
{
var properties = new AuthenticationProperties { RedirectUri = RedirectUri };
if (UserId != null)
{
properties.Dictionary[XsrfKey] = UserId;
}
context.HttpContext.GetOwinContext().Authentication.Challenge(properties, LoginProvider);
}
在ManageController.LinkLoginCallback()
中,这就是我们的
var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync(XsrfKey, User.Identity.GetUserId());
是否注意到挑战密钥为Dictionary[XsrfKey] = UserId
?这意味着当状态参数被序列化时,它将包含一对"XsrfId" : {user id}
的键值对,这将成为我们的防伪令牌。
虽然很难对所有用户发起大规模攻击,但是针对特定用户应该相对简单,因为CSCF令牌非常可预测。我是否缺少某些东西,或者这是安全漏洞?