2011-04-20 59 views
1

我正在使用以下代碼,我從網上找到用於驗證用戶的網站。但是用戶必須點擊一個按鈕才能登錄到該網站,即使他們登錄到Gmail。我想要做的是當他們登錄到Gmail時自動登錄他們。 那麼我如何修改下面的代碼來做到這一點?如何使用DotNetOpenAuth來檢測用戶是否登錄到谷歌

static string openidurl = "https://www.google.com/accounts/o8/id"; 
protected void Page_Load(object sender, EventArgs e) 
{ 
    //The Response 
    OpenIdRelyingParty openid = new OpenIdRelyingParty(); 

    var response = openid.GetResponse(); 
    if (response != null) 
    { 
      switch (response.Status) 
      { 
       case AuthenticationStatus.Authenticated: 

       var fetch = response.GetExtension<FetchResponse>(); 
       string email = ""; 
       if (fetch != null) 
       { 
        email = fetch.GetAttributeValue(WellKnownAttributes.Contact.Email); 
       } 
       break; 
       } 
    } 
} 

private void CreateRequest() 
{ 
    using (OpenIdRelyingParty openid = new OpenIdRelyingParty()) 
    { 
     IAuthenticationRequest request = openid.CreateRequest(openidurl); 

     var fetch = new FetchRequest(); 
     fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email); 
     request.AddExtension(fetch); 

     // Send your visitor to their Provider for authentication. 
     request.RedirectToProvider(); 
    } 
} 

protected void Button1_Click(object sender, EventArgs e) 
{ 
    CreateRequest(); 
} 

回答

0

你是conflating兩件事 - 谷歌是一個OpenID提供商,谷歌用戶登錄谷歌。

將Google用作OpenId提供程序意味着您可以驗證用戶是否是他們自稱的用戶。

這與Google用戶登錄Google不同 - 這不是Google作爲OpenId提供商公開的內容,因爲它不會影響用戶的身份。

總之,你不能做你想做的事情,而不是通過OpenId和DotNetOpenAuth。

+0

我想用OpenID並不是解決我的問題的正確方法。 在stackoverflow網站本身有一個Gmail登錄選項。由於我已經登錄到gmail,它沒有要求再次登錄到gmail。 我該如何做類似的事情?我正在使用ASP .NET – Janaka 2011-04-20 07:46:41

+0

@Janaka - 不,沒有。有一種方法可以使用Google作爲OpenId提供商登錄到StackOverflow。我相信你所描述的是由圖書館和谷歌自動完成的(所以谷歌知道你已經登錄,因此經過認證並告訴你沒有你需要再次登錄的你是誰)。 – Oded 2011-04-20 07:48:41

+1

好的,謝謝。我的問題是爲什麼自動登錄不會發生,即使我登錄到Gmail。 原因是我從Crome登錄到Gmail,我正在使用IE調試應用程序 – Janaka 2011-04-20 07:59:01