2013-06-05 63 views
1

我有一個ASP.NET MVC 4應用程序,允許用戶使用Gmail等外部服務登錄。使用oauth從外部登錄服務(Gmail,臉書)註銷

到目前爲止,用戶可以在應用程序內部登錄和導航。但問題是在註銷。我有一個註銷該請求的按鈕,在我的AccountController中調用控制器操作LogOff()。在該方法中,如果用戶通過oauth進行身份驗證,如何註銷?

本地帳戶,我用:

public ActionResult LogOff() 
     { 
      WebSecurity.Logout(); 
      return RedirectToAction("Login", "Account"); 
     } 

但通過OAuth我沒有看到類似的事情...... 我想我需要清除某種餅乾的,但我不知道如何...

回答

2

基於this,我採取了以下客戶端解決方案(我問以前,如果用戶想也登出的提供者):

//get accountType, accessToken, redirectUrl and clientID 
var accountType = ...; 
var accessToken = ...; 
var redirectUrl = ...; 
var clientID = ...; 
$("#logoutConfirmButton").on('click', function() { 
    externalLogout(); 
}); 

function externalLogout() { 
    var url, params; 
    if (accountType== "facebook") { 
     url = "https://www.facebook.com/logout.php"; 
     params = { 
      next: redirectUrl, 
      access_token: encodeURIComponent(accessToken) 
     }; 
     performCallLogout(url, params, accountType); 
    } else if (accountType== "google") { 
     url = "https://www.google.com/accounts/Logout?continue=https://appengine.google.com/_ah/logout"; 
     params = { 
      next: redirectUrl 
     } 
     performCallLogout(url, params, accountType); 
    } else if (accountType == "microsoft") { 
     url = "https://login.live.com/oauth20_logout.srf"; 
     params = { 
      clientId: clientID, 
      redirectUrl: redirectUrl 
     } 
     performCallLogout(url, params, accountType); 
    } 
} 

function performCallLogout(url, params, accountType) { 
    if (accountType == "facebook") { 
     window.location.href = url + "?next=" + params.next + "&access_token=" + params.access_token; 
    } else if (accountType == "google") { 
     window.location.href = url + "?continue=" + params.next; 
    } else if (accountType == "microsoft") { 
     window.location.href = url + "?client_id=" + params.clientId + "&redirect_url=" + params.redirectUrl; 
    } 
} 

希望這有助於別人。

+0

你在哪裏以及如何添加這個,即時消息總共mvc noob(3小時)添加這個,因爲我有完全相同的問題 – Slartibartfast

+0

@Slartibartfast您可以在'

1

WebSecurity.Logout();將註銷用戶,即使他們通過OAuth進行身份驗證。

如果你想確保令牌註銷後不會保留你可以調用

Session.Remove("facebooktoken"); //Facebook example

的信息是從this網頁。還有一些值得一讀的細節。

+0

謝謝,但刪除會話值的解決方案沒有工作...當我登出我的應用程序,並嘗試登錄與另一個帳戶我不能,我直接用以前的帳戶登錄.. 。但是,如果我註銷,例如,在Facebook頁面中,我可以使用另一個帳戶登錄...並且Google服務在ExtraData中默認沒有該令牌... – amp

+0

好的,所以你想要能夠從您的應用中清除第三方Cookie?如果您想添加登錄方法,您可以在帳戶管理頁面中關聯其他帳戶。 – James

+0

是的,我認爲清理cookie應該可以解決問題......我該怎麼做?你有一些基本的例子嗎? – amp

1

聽起來像您想要將用戶從源驗證站點註銷?只有認證站點才能刪除/修改其Cookie。

解決方案是將用戶重定向到身份驗證站點的註銷頁面,或者使用API​​腳本將用戶註銷(如果該站點存在)。您可以使用帶有「目標」屬性打開一個新窗口,如果你不希望主瀏覽器窗口重定向。

臉譜,例如,有一個API調用:

FB.logout(function(response) { // user is now logged out });

的MVC Facebook的客戶端有一個方法GetLogoutUrl,也返回你可以在服務器端使用的URL。