c# - 如何生成 OAuth 2 Client Id 和 Secret

标签 c# .net vb.net web-services oauth

我想使用 .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/

相关文章:

c# - 正则表达式与 C# 中的交集匹配

C# CSOM - 检查文档库中是否存在文件

c# - 将 C# 泛型类型作为参数传递

.net - 为什么 .NET 中的 bool 值是 4 个字节?

c# - 使 .net 4 应用程序在 .net 3.5 上兼容

php - 连接到客户端桌面应用程序的网站

.net - 使用.net 3.5框架快速排序

c# - 如何在没有互操作程序集的情况下将任何文件类型嵌入到 Microsoft Word 中

mysql - 将表项列为 VB 的字符串

c# - 套接字监听器同时进行多个 AcceptAsync 调用