4

DotNetOpenAuth プロジェクトの実験を始めたところです。サンプルのOpenIdRelyingPartyMvcプロジェクトを変更すると、Google と連携するClaimRequestためのメールを取得できました。

しかし、自分のプロジェクトに 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();
}

}

4

1 に答える 1

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 はユーザーの認証を拒否することに注意してください。そのため、メール アドレスが実際に必要ない場合は、オプションとしてマークし、Google ユーザーからメール アドレスを取得するのを控えて、ユーザーがメール アドレスを放棄するよう強制してユーザーを追い出さないようにすることをお勧めします。したくない。

于 2009-10-20T10:16:50.727 に答える