2009-09-27 4 views
4

방금 ​​DotNetOpenAuth 프로젝트를 실험하기 시작했습니다. 샘플 OpenIdRelyingPartyMvc 프로젝트를 수정, 나는 구글 작업 할 ClaimRequest에 대한 이메일을 얻을 수있었습니다. 난 내 자신의 프로젝트에 오픈 ID를 추가하려고 할 때내 ClaimsRequest가 null로 돌아 오는 이유는 무엇입니까?

그러나 ClaimResponse 항상 다시 널 (null) 제공됩니다. 제가 놓친 프로젝트 나 환경 설정이 있는지 궁금합니다.

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에 설정 정보를 추가

여기 내 Authenticate 방법입니다.

Google은 '선택 사항'으로 표시된 모든 속성 요청을 무시한다는 점에서 고유 한 특성이 있습니다. Google의 이메일 주소를 받으려면 사용자의 이메일 주소를 '필수'로 요청해야합니다. 그러나 사용자가 전자 메일 주소를 기꺼이 포기하지 않는 한, 필요한대로 특성을 표시하여 사용자 인증을 거부합니다. 따라서 실제로 이메일 주소가 필요하지 않은 경우 선택 사항으로 표시하고 Google 사용자로부터 이메일 주소를 가져 오는 것이 가장 좋습니다. 그러면 사용자가 이메일 주소를 포기하도록하여 강제로 사용자를 추방하지 않도록 할 수 있습니다. 그러고 싶지 않아.

관련 문제