我正在创建我自己的授权提供程序,它扩展自 OAuthAuthorizationServerProvider。
public class AsyncAuthorizationProvider : OAuthAuthorizationProvider {
//...
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) {
string username = context.UserName; //Returns the username sent in the request.
string password = context.Password; //Returns the password sent in the request.
//...
}
}
如您所见,OAuthGrantResourceOwnerCredentialsContext 的上下文已经带有两个用于检索用户名和密码的有用属性。
当用户请求我的 oauth 端点时,此信息由用户发送:
{
"grant_type": "password",
"client_id": "someClientId",
"client_secret": "someClientSecret",
"username": "myUsername",
"password": "myPassword"
}
此 JSON 存储在 OAuth 上下文中,但是如果我想发送我的 userId 而不是我的 username 怎么办?如果我的应用程序允许重复的用户名并且我想使用 id 来唯一标识一个用户名怎么办?
当然,我可以将我的 Id 作为 "username": "myUserId" 发送。但这似乎不对。
username 应该包含一个username。这可能会使消费者感到困惑。
有没有办法发送 userId 并从上下文中检索它?
请您参考如下方法:
您可以设置任何您想要的参数并获取它如下:
var formCollection = (Microsoft.Owin.FormCollection)context.Parameters;
var userId = formCollection.Get("userId");
