2009-09-27 102 views
4

我刚开始尝试DotNetOpenAuth项目。修改样本OpenIdRelyingPartyMvc项目,我能够获得ClaimRequest电子邮件与谷歌合作。为什么我的ClaimsRequest会返回null?

但是,当我尝试将OpenID添加到我自己的项目中时,ClaimResponse始终返回null。我想知道是否有我缺少的项目或环境设置?

这里是我的Authenticate方法:

public ActionResult Authenticate(string returnUrl) 
{ 
    var response = openid.GetResponse(); 
    if (response == null) 
    { 
     // Stage 2: user submitting Identifier 
     Identifier id; 
     if (Identifier.TryParse(Request.Form["openid_identifier"], out id)) 
     { 
      try 
      { 
       IAuthenticationRequest req = openid.CreateRequest(Request.Form["openid_identifier"]); 
       req.AddExtension(new ClaimsRequest { Email = DemandLevel.Require }); 
       return req.RedirectingResponse.AsActionResult(); 
      } 
      catch (ProtocolException ex) 
      { 
       ViewData["Message"] = ex.Message; 
       return View("Login"); 
      } 
     } 
     else 
     { 
      ViewData["Message"] = "Invalid identifier"; 
      return View("Login"); 
     } 
    } 
    else 
    { 
     // Stage 3: OpenID Provider sending assertion response 
     switch (response.Status) 
     { 
      case AuthenticationStatus.Authenticated: 
       ClaimsResponse sreg = response.GetExtension<ClaimsResponse>(); 
       if (sreg != null) 
       { 
        var email = sreg.Email; 
        Session["Email"] = email; 
       } 
       Session["FriendlyIdentifier"] = response.FriendlyIdentifierForDisplay; 
       FormsAuthentication.SetAuthCookie(response.ClaimedIdentifier, false); 
       if (!string.IsNullOrEmpty(returnUrl)) 
       { 
        return Redirect(returnUrl); 
       } 
       else 
       { 
        return RedirectToAction("Index", "Home"); 
       } 
      case AuthenticationStatus.Canceled: 
       ViewData["Message"] = "Canceled at provider"; 
       return View("Login"); 
      case AuthenticationStatus.Failed: 
       ViewData["Message"] = response.Exception.Message; 
       return View("Login"); 
     } 
    } 
    return new EmptyResult(); 
} 

}

回答

11
<configuration> 
     <configSections> 
      <section name="dotNetOpenAuth" type="DotNetOpenAuth.Configuration.DotNetOpenAuthSection" requirePermission="false" allowLocation="true"/> 
     </configSections> 
     <dotNetOpenAuth> 
      <openid> 
      <relyingParty> 
       <behaviors> 
        <!-- The following OPTIONAL behavior allows RPs to use SREG only, but be compatible 
         with OPs that use Attribute Exchange (in various formats). --> 
        <add type="DotNetOpenAuth.OpenId.Behaviors.AXFetchAsSregTransform, DotNetOpenAuth" /> 
       </behaviors> 
      </relyingParty> 
      </openid> 
     </dotNetOpenAuth> 
    </configuration> 

http://dotnetopenauth.net:8000/wiki/CodeSnippets/OpenIDRP/AXFetchAsSregTransform

添加的配置信息,以你的web.config。

谷歌有一个独特的特质,因为它忽略了标记为“可选”的所有属性请求。您必须要求用户的电子邮件地址为“必填”,才能从Google获取电子邮件地址。请注意,根据需要标记该属性,Google将拒绝验证用户,除非用户愿意放弃其电子邮件地址。因此,如果您实际上并不需要电子邮件地址,最好将其标记为可选,然后放弃Google用户,以避免强制用户放弃其电子邮件地址不想。