【问题标题】:OWIN + OAuth - How to provide User ID instead of UserNameOWIN + OAuth - 如何提供用户 ID 而不是用户名
【发布时间】:2015-01-24 19:28:24
【问题描述】:

我正在创建自己的授权提供程序,它从 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 从上下文中检索它?

【问题讨论】:

    标签: c# asp.net .net oauth owin


    【解决方案1】:

    您可以设置任何您想要的参数并获取如下:

    var formCollection = (Microsoft.Owin.FormCollection)context.Parameters;
    
    var userId = formCollection.Get("userId");
    

    【讨论】:

      猜你喜欢
      • 2017-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-07
      • 2017-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多