|
@@ -84,7 +84,7 @@ public class TimestampNonceManager : IDisposable
|
|
: TimestampNonceResult.ReplayAttackDetected;
|
|
: TimestampNonceResult.ReplayAttackDetected;
|
|
}
|
|
}
|
|
|
|
|
|
- /// <summary> 验证nonce并提取payload: null时间误差过大,false检测到重放攻击,true没问题 </summary>
|
|
|
|
|
|
+ /// <summary> 验证nonce并提取payload </summary>
|
|
public (TimestampNonceResult, DateTimeOffset timestamp, ReadOnlyMemory<byte> payload) CheckValidAndExtractPayload(ReadOnlyMemory<byte> payloadAndNonce)
|
|
public (TimestampNonceResult, DateTimeOffset timestamp, ReadOnlyMemory<byte> payload) CheckValidAndExtractPayload(ReadOnlyMemory<byte> payloadAndNonce)
|
|
{
|
|
{
|
|
ThrowIfDisposed();
|
|
ThrowIfDisposed();
|
|
@@ -99,7 +99,7 @@ public class TimestampNonceManager : IDisposable
|
|
// 检查时间戳是否在允许的时间窗口内, 超过最大时间差则拒绝
|
|
// 检查时间戳是否在允许的时间窗口内, 超过最大时间差则拒绝
|
|
if (Math.Abs((DateTimeOffset.UtcNow - timestamp).TotalMilliseconds) > _maxTimeSkew.TotalMilliseconds) return (TimestampNonceResult.TimestampSkew, timestamp, payload);
|
|
if (Math.Abs((DateTimeOffset.UtcNow - timestamp).TotalMilliseconds) > _maxTimeSkew.TotalMilliseconds) return (TimestampNonceResult.TimestampSkew, timestamp, payload);
|
|
|
|
|
|
- // 如果 nonce 已存在,则为重放攻击,返回 false;否则添加并返回 true
|
|
|
|
|
|
+ // 如果 nonce 已存在,则为重放攻击
|
|
return (_holds.TryAdd(nonce, DateTime.UtcNow) ? TimestampNonceResult.OK : TimestampNonceResult.ReplayAttackDetected, timestamp, payload);
|
|
return (_holds.TryAdd(nonce, DateTime.UtcNow) ? TimestampNonceResult.OK : TimestampNonceResult.ReplayAttackDetected, timestamp, payload);
|
|
}
|
|
}
|
|
|
|
|