2014-01-16 75 views
3

我有一個功能可以創建語音郵件,並且如果通過Twitter進行身份驗證,則會向他們的Twitter帳戶發佈消息。我希望用戶可以根據需要關閉發送給Twitter的消息,所以我很好奇是否有辦法清除證書。我跟着從LinqToTwitter文檔的例子:Linq到Twitter清除IOAuthCredentials憑據

IOAuthCredentials credentials = new SessionStateCredentials(); 

     if (credentials.ConsumerKey == null || credentials.ConsumerSecret == null) 
     { 
      credentials.ConsumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"]; 
      credentials.ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"]; 
     } 

     auth = new WebAuthorizer 
     { 
      Credentials = credentials, 
      PerformRedirect = authUrl => Response.Redirect(authUrl) 
     }; 

     if (!Page.IsPostBack && Request.QueryString["oauth_token"] != null) 
     { 
      auth.CompleteAuthorization(Request.Url); 
     } 

     if (auth.IsAuthorized) 
     { 
      twitterCtx = new TwitterContext(auth); 
      Session["TwitterContext"] = twitterCtx; 
      twLoginButton.Text = "Logout of Twitter"; 
     } 

我試過下面的代碼和變化:

credentials = null; 

SessionStateCredentials credentials = Dispose(); 

但它顯示了每個的這些錯誤。我希望有人能指導我清除

IOAuthCredentials credentials = new SessionStateCredentials(); 

這是我認爲需要發生的事情。任何意見,將不勝感激。

回答

2

SessionStateCredentials類型具有使用會話狀態作爲其後備存儲的屬性。這裏有幾個選項,每個選項都有其優點和缺點:

  1. 將屬性設置爲null。例如

    credentials.ConsumerKey = null; credentials.ConsumerSecret = null; //等等......

這是難看一點,但你可以寫封裝語句的方法。

  1. 清除各個會話狀態項目。例如

    Session.Remove(「ConsumerKey」); Session.Remove(「ConsumerSecret」); //等等...

這更明確。但是,它會打破現有的封裝並迫使您獲得對當前會話的引用。

  1. 使用Clear方法從SessionStateCredentials派生一個新類,該方法執行前述方法之一的步驟。這可能是最乾淨的選項。

這裏有一個鏈接到SessionStateCredentials類,所以你可以看到內部的實現:

http://linqtotwitter.codeplex.com/SourceControl/latest#LinqToTwitter/OAuth/SessionStateCredentials.cs

+0

我試圖清除後面的按鈕點擊代碼證書和所使用的Session.Remove理念。當我嘗試重新進行身份驗證時,會出現401錯誤。你能否通過獲得當前會議的參考來解釋你的意思?我很感激。 –

+0

如果你的邏輯是在一個庫中,你需要添加一個項目引用System.Web.dll,爲System.Web添加一個using語句,並引用HttpContext.Current.Session屬性(就像我在答案鏈接)。由於您處於代碼隱藏狀態,如您所知,您可以說Session。401的原因是因爲你可能已經清除了ConsumerKey和ConsumerSecret。我不知道你在做什麼,所以我在回答中提到了這些。如果您想用相同的授權人重新進行身份驗證,請單獨保留ConsumerKey和ConsumerSecret,並僅清除OAuthToken和AccessToken。 –