为什么没有关于openID用户的信息通过该协议?

时间:2009-09-08 12:18:49

标签: asp.net-mvc openid dotnetopenauth

我正在使用DotNetOpenAuth在我们的网络应用程序中集成openID。以下代码向提供商请求信息。

try
{
  var req = openid.CreateRequest(Request.Form["openid_identifier"]);
  req.AddExtension(new DotNetOpenAuth.OpenId.Extensions.SimpleRegistration.ClaimsRequest
  {
    Email = DotNetOpenAuth.OpenId.Extensions.SimpleRegistration.DemandLevel.Require,
    FullName = DotNetOpenAuth.OpenId.Extensions.SimpleRegistration.DemandLevel.Require,
    Nickname = DotNetOpenAuth.OpenId.Extensions.SimpleRegistration.DemandLevel.Request,
    PostalCode = DotNetOpenAuth.OpenId.Extensions.SimpleRegistration.DemandLevel.Request
  });

  return req.RedirectingResponse.AsActionResult();
}

由于某种原因,openID提供商的响应永远不会附带我要求的信息。以下是代码:

// Stage 3: OpenID Provider sending assertion response
switch (response.Status) {
  case AuthenticationStatus.Authenticated:
    Session["FriendlyIdentifier"] = response.FriendlyIdentifierForDisplay;
    FormsAuthentication.SetAuthCookie(response.ClaimedIdentifier, false);
    if (!string.IsNullOrEmpty(returnUrl)) {
       return Redirect(returnUrl);
    } else {
       return RedirectToAction("Index", "Home");
    }

我已经尝试过:response.ClaimedIdentifier以百万种方式而且从来没有我可以做的有价值的信息。有什么想法吗?

1 个答案:

答案 0 :(得分:6)

IAuthenticationResponse.ClaimedIdentifier属性从不包含您要求的这些属性。它只包含OpenID用户的“用户名”。

您正在完美地发送请求。只需添加一点来处理积极响应:

// Stage 3: OpenID Provider sending assertion response
switch (response.Status) { 
  case AuthenticationStatus.Authenticated:
    Session["FriendlyIdentifier"] = response.FriendlyIdentifierForDisplay; 
    FormsAuthentication.SetAuthCookie(response.ClaimedIdentifier, false);
    var sreg = response.GetExtension<ClaimsResponse>();
    if (sreg != null) { // the Provider MAY not provide anything
      // and even if it does, any of these attributes MAY be missing
      var email = sreg.Email;
      var fullName = sreg.FullName;
      // get the rest of the attributes, and store them off somewhere.
    }
    if (!string.IsNullOrEmpty(returnUrl)) {
      return Redirect(returnUrl);
    } else {
       return RedirectToAction("Index", "Home");
    }
  break;
  // ...