【发布时间】:2022-10-06 00:13:39
【问题描述】:
我创建了 RemoteAuthenticationHandler,它看起来像这样:
public class AuthAndAuthHandler : RemoteAuthenticationHandler<AuthAndAuthSchemeOptions>
{
public AuthAndAuthHandler(IOptionsMonitor<AuthAndAuthSchemeOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock)
: base(options, logger, encoder, clock)
{
}
protected override async Task HandleChallengeAsync(AuthenticationProperties properties)
{
var rng = RandomNumberGenerator.Create();
var state = new byte[128];
var nonce = new byte[128];
var codeVerifier = new byte[64];
rng.GetBytes(state);
rng.GetBytes(nonce);
rng.GetBytes(codeVerifier);
var codeChallenge = SHA256.HashData(codeVerifier);
Response.Cookies.Append(\"Nonce\", Convert.ToBase64String(SHA256.HashData(nonce)), new CookieOptions
{
Path = \"/callback\",
HttpOnly = true,
IsEssential = true,
Secure = true,
SameSite = SameSiteMode.Strict,
Expires = Clock.UtcNow.AddHours(1)
});
Response.Redirect($\"{Options.Authority}/authorization?client_id={Options.ClientId}\" +
$\"&callback_uri={Request.Scheme}://{Request.Host}{Options.CallbackPath}&scopes={Options.Scopes}\" +
$\"&state={Convert.ToBase64String(state)}&nonce={Convert.ToBase64String(nonce)}&code_challenge={Convert.ToBase64String(codeChallenge)}\");
}
protected override async Task<HandleRequestResult> HandleRemoteAuthenticateAsync()
{
throw new NotImplementedException();
}
}
在HandleRemoteAuthenticateAsync() 方法中,我必须验证状态,在远程授权成功后我会得到。当挑战之后我失去了早期生成的状态和代码验证器时,我该怎么做?
-
可以分享更多代码吗?HandleChallengeAsync 方法是用来处理 401 挑战问题的,为什么你认证成功并进入这个方法?
-
也许我含糊地表达了自己。假设是收到401后,我打电话给
HandleChallengeAsync。如果用户在重定向到/authorization 后登录,他应该被重定向到/callback,这里会调用HandleRemoteAuthenticationAsync方法。而现在我不知道如何验证状态参数,我将获得它作为查询参数 dla w /callback,因为我不再有权访问我在HandleChallengeAsync中创建的状态。也许我的方式是错误的?
标签: c# asp.net-core asp.net-core-authenticationhandler