【发布时间】:2014-03-18 14:09:59
【问题描述】:
我看到已经有一些关于此的问题,但我发现没有一个涉及任何细节。
我之前使用过我自己的 DotNetOpenAuth 代码,但现在我决定切换到 Microsoft Wrapper 进行身份验证。无论如何,我发现这个非常好的 OAuth 客户端:
https://github.com/mj1856/DotNetOpenAuth.GoogleOAuth2
它似乎工作正常,但现在它涉及迁移部分。在我当前的登录系统中,我保存了 Google 返回的完整 OpenID URL,格式为:
https://www.google.com/accounts/o8/id?id=????????????????????????????????????
根据https://developers.google.com/accounts/docs/OpenID 此处的文档,我应该能够通过新的 OAuth 系统以某种方式获得该值。
我在 Auth 请求中包含了“openid.realm”参数。
return BuildUri(AuthorizationEndpoint, new NameValueCollection
{
{ "response_type", "code" },
{ "client_id", _clientId },
{ "scope", string.Join(" ", scopes) },
{ "redirect_uri", returnUrl.GetLeftPart(UriPartial.Path) },
{ "state", state },
{ "openid.realm", "http://myoldopenidrealm" }
});
据我了解,文档应该是我需要做的所有事情。我已确保用于我的 OpenID 2 身份验证的 Realm 与我的返回 URL 相同。
完成后,我执行该令牌请求,据我了解,在这里我应该看到一个“open_id”字段,但我不明白如何获取它。
protected override string QueryAccessToken(Uri returnUrl, string authorizationCode) {
var postData = HttpUtility.ParseQueryString(string.Empty);
postData.Add(new NameValueCollection
{
{ "grant_type", "authorization_code" },
{ "code", authorizationCode },
{ "client_id", _clientId },
{ "client_secret", _clientSecret },
{ "redirect_uri", returnUrl.GetLeftPart(UriPartial.Path) },
});
var webRequest = (HttpWebRequest)WebRequest.Create(TokenEndpoint);
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
using (var s = webRequest.GetRequestStream())
using (var sw = new StreamWriter(s))
sw.Write(postData.ToString());
using (var webResponse = webRequest.GetResponse()) {
var responseStream = webResponse.GetResponseStream();
if (responseStream == null)
return null;
using (var reader = new StreamReader(responseStream)) {
var response = reader.ReadToEnd();
var json = JObject.Parse(response);
var accessToken = json.Value<string>("access_token");
return accessToken;
}
}
}
这是文档所说的,我看不到“sub”或“openid_id”字段。
*该令牌请求的响应包括常用字段(access_token 等),加上一个 openid_id 字段和标准 OpenID Connect 子字段。在此上下文中您需要的字段是 openid_id 和 sub:*
【问题讨论】:
标签: c# oauth openid dotnetopenauth google-oauth