想通过DotNetOpenID获取用户详细信息

时间:2011-03-05 10:59:22

标签: asp.net openid dotnetopenauth

我正在使用DotOpenID,我想获取用户的昵称和电子邮件ID

请求

 protected void loginButton_Click(object sender, EventArgs e)
{

    if (!openidValidator.IsValid) return; // don't login if custom validation failed.  
    OpenID(openid_identifier.Text);
}

private void OpenID(string Indentifier)
{
    OpenIdRelyingParty openid = new OpenIdRelyingParty();
    try
    {
        IAuthenticationRequest request = openid.CreateRequest(Indentifier);
        // Send your visitor to their Provider for authentication.  
        ClaimsRequest fetch = new ClaimsRequest();
        fetch.FullName = DemandLevel.Require;
        fetch.Email = DemandLevel.Require;
        request.AddExtension(fetch);
        request.RedirectToProvider();
    }
    catch (Exception ex)
    {
        // The user probably entered an Identifier that   
        // was not a valid OpenID endpoint.  
        openidValidator.Text = ex.Message;
        openidValidator.IsValid = false;
    }
}

和回复

 openid_identifier.Focus();
    OpenIdRelyingParty openid = new OpenIdRelyingParty();
    if (openid.Response != null)
    {
        switch (openid.Response.Status)
        {
            case AuthenticationStatus.Authenticated:

                string email = "";
                string alias = "";

                ClaimsResponse fetch = openid.Response.GetExtension(typeof(ClaimsResponse)) as ClaimsResponse;
                alias = fetch.Nickname;
                email = fetch.Email;

                if (string.IsNullOrEmpty(alias))
                    alias = openid.Response.ClaimedIdentifier;
                if (string.IsNullOrEmpty(email))
                    email = openid.Response.ClaimedIdentifier;


                FormsAuthentication.RedirectFromLoginPage(openid.Response.ClaimedIdentifier, chkRememberMe.Checked);
                break;
            case AuthenticationStatus.Canceled:
                loginCanceledLabel.Visible = true;
                break;
            case AuthenticationStatus.Failed:
                loginFailedLabel.Visible = true;
                break;

        }
    }

但是我在这里得到异常“对象引用未设置为对象的实例”。

 ClaimsResponse fetch = openid.Response.GetExtension(typeof(ClaimsResponse)) as ClaimsResponse;
                alias = fetch.Nickname;
                email = fetch.Email;

1 个答案:

答案 0 :(得分:2)

可能重复:
claimsResponse Return Null
https://stackoverflow.com/questions/3265678/dotnetopenid-claimsresponse-always-null

第一个答案由安德鲁·阿诺特提出,可能与此有关:

  

看起来你正在做的一切   对。在这一点上它取决于   您正在使用的提供商。哪个是   你测试?有些人没有   支持简单注册   (ClaimsRequest)。仅限其他人   支持列入白名单的RP。然后   当你的RP时,其他人不支持它   在“localhost”。

     

我的建议:对myopenid.com进行测试,   因为这看起来很好,一致   行为和对Simple的支持   注册延期。但你的RP   必须始终准备好接受   对于ClaimsResponse,null,因为你是   从来没有保证OP会给你   任何东西。

     

即使您获得非null结果,   您要求的各个字段   (即使你要求他们标明)也许   为空或空白。