我想使用 .NET 生成客户端 ID 和客户端密码。我阅读了 OAuth 2 规范,例如,那里没有指定客户端 secret 的大小。是否有使用 .NET 框架生成客户端 ID 和客户端密码的良好做法???
最佳答案
作为section 2.2 OAuth 2.0 授权框架的作者说:
The authorization server issues the registered client a client identifier -- a unique string representing the registration information provided by the client. The client identifier is not a secret; it is exposed to the resource owner and MUST NOT be used alone for client authentication. The client identifier is unique to the authorization server.
The client identifier string size is left undefined by this specification. The client should avoid making assumptions about the identifier size. The authorization server SHOULD document the size of any identifier it issues.
所以你可以自己定义客户端标识符。这取决于你的选择。您可以使用 System.Guid
简单地生成一个,或者使用 uid + systemTime,也可以对其进行哈希、加密或任何您想要的其他内容。
但是客户端 secret 应该是一个加密强度高的随机字符串。你可以这样生成一个:
RandomNumberGenerator cryptoRandomDataGenerator = new RNGCryptoServiceProvider();
byte[] buffer = new byte[length];
cryptoRandomDataGenerator.GetBytes(buffer);
string uniq = Convert.ToBase64String(buffer);
return uniq;
您也可以使用加密哈希函数() 来哈希UUID+SystemTime+somthingelse 来自己实现。
想了解更多做法可以引用some open implementations from here.
关于c# - 如何生成 OAuth 2 Client Id 和 Secret,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23652166/