asp.net-mvc-4 - 使用 oauth 从外部登录服务(Gmail、facebook)注销

标签 asp.net-mvc-4 oauth-2.0 logout

我有一个 ASP.NET MVC 4 应用程序,允许用户使用 Gmail 等外部服务登录。

到目前为止,用户可以登录并在应用程序内导航。但问题出在注销上。我有一个注销按钮,该按钮请求在我的 AccountController 内调用 Controller 操作 LogOff()。在该方法中,如果用户通过 oauth 进行身份验证,我如何注销?

对于本地帐户,我使用:

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

但是对于 oauth 我没有看到任何类似的东西...... 我想我需要清除某种 cookie,但我不知道如何...

最佳答案

基于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;
    }
}

希望这对某人有帮助。

关于asp.net-mvc-4 - 使用 oauth 从外部登录服务(Gmail、facebook)注销,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16946798/

相关文章:

c# - 如何防止根据条件执行 Controller 操作?

asp.net-mvc - 在局部 View 中渲染 umbraco 场

c# - 创建自定义 OAuth2 服务器

android - 无法在 Google Developer Console 中注册 Proximity API

c# - "TransformXml"任务意外失败

jquery - 基于另一个 Textbox MVC 4 填充文本框

java - 带有 Spring Security 的 Amazon Cognito Oauth2

来自 django.contrib.auth.views 的 Python 导入注销 ImportError : cannot import name 'logout'

php - 如何使用 Laravel 5.3 注销并重定向到登录页面?

session - Keycloak 注销请求不会注销用户