2012-07-13 105 views
2

我使用DotNetOpenAuth和MVC 3.我可以使用OpenId進行身份驗證。DotNetOpenAuth如何從OpenId檢索電子郵件和名稱

所以我的URL指向控制器具有附加的OpenID =

下面是一個例子變量:

https://www.google.com/accounts/o8/id?id=DFtOawDkCUoLb3YxPzmrEI59-JiSZiAeR-NWw-0

http://mysite.com/Account/Register?OpenID= .......

如何如何能我收到OpenId帳戶的電子郵件和名稱並顯示給View?

switch (response.Status) 
      { 
       case AuthenticationStatus.Authenticated: 
        LogOnModel lm = new LogOnModel(); 
        lm.OpenID = response.ClaimedIdentifier; 
        // Check if user exist 
        MembershipUser user = MembershipService.GetUser(lm.OpenID); 
        if (user != null) 
        { 
         lm.UserName = user.UserName; 
         FormsService.SignIn(user.UserName, false); 
        } 

        return View("LogOn", lm); 

       case AuthenticationStatus.Canceled: 
        ViewBag.Message = "Canceled at provider"; 
        return View("LogOn"); 
       case AuthenticationStatus.Failed: 
        ViewBag.Message = response.Exception.Message; 
        return View("LogOn"); 
      } 

回答

3
var fetchResponse = response.GetExtension<FetchResponse>(); 
string email = String.Empty; 
if (fetchResponse != null) 
{ 
    email = fetchResponse.GetAttributeValue(WellKnownAttributes.Contact.Email); 
} 

但不要忘了發送請求時,要求它使用戶可以授權你不得不在與他的OpenID提供商的身份驗證階段這樣的信息:

IAuthenticationRequest request = openid.CreateRequest(openidurl); 
var fetchRequest = new FetchRequest(); 
fetchRequest.Attributes.AddRequired(WellKnownAttributes.Contact.Email); 
request.AddExtension(fetch); 
request.RedirectToProvider(); 
相關問題