C# WebAPI OAuth注销-如何删除令牌Cookie?

C# WebAPI OAuth注销-如何删除令牌Cookie?,c#,cookies,asp.net-web-api,oauth-2.0,C#,Cookies,Asp.net Web Api,Oauth 2.0,我有一个带有OAuth登录名的WebAPI,配置如下: app.UseOpenIdConnectAuthentication( new OpenIdConnectAuthenticationOptions { ClientId = clientId, Authority = authority, PostLogoutRedirectUri = "https://www.microsoft.com/", Notific

我有一个带有OAuth登录名的WebAPI,配置如下:

app.UseOpenIdConnectAuthentication(
    new OpenIdConnectAuthenticationOptions
    {
        ClientId = clientId,
        Authority = authority,
        PostLogoutRedirectUri = "https://www.microsoft.com/",
        Notifications = new OpenIdConnectAuthenticationNotifications
        {
            AuthenticationFailed = context =>
            {
                context.HandleResponse();
                context.Response.Redirect("/Error?message=" + context.Exception.Message);
                return Task.FromResult(0);
            }
        }
    });
并对所有使用

config.Filters.Add(new System.Web.Http.AuthorizeAttribute());
现在我想添加一个名为LogoutController的ApicController(猜猜它是做什么的)

使用

但我并不是那样从WebAPI注销的。我没有找到任何关于如何从WebAPI注销的信息。但是我发现,代码又是MVC,似乎我无法将
HttpCookie
放入我的
HttpResponseMessage
对象:

    [HttpGet]
    public HttpResponseMessage Logout()
    {
        FormsAuthentication.SignOut();

        // clear authentication cookie
        HttpCookie cookie1 = new HttpCookie(FormsAuthentication.FormsCookieName, "");
        cookie1.Expires = DateTime.Now.AddYears(-1);

        var response = Request.CreateResponse(HttpStatusCode.OK);
        response.Content = new StringContent("<html><title>Logout successful</title><body style=\"font-family:sans-serif\"><div style=\"display:table; width:100%; height:100%; margin:0; padding:0; \"><div style=\"display:table-cell; vertical-align:middle; text-align:center;\">You have been successfully logged out.<br>You can close this window/tab now.</div></div></body></html>");
        response.Headers.AddCookies(cookie1); // Types don't match
        return response;
    }
[HttpGet]
公共HttpResponseMessage注销()
{
FormsAuthentication.SignOut();
//清除身份验证cookie
HttpCookie cookie1=新的HttpCookie(FormsAuthentication.FormScookeName,“”);
cookie1.Expires=DateTime.Now.AddYears(-1);
var response=Request.CreateResponse(HttpStatusCode.OK);
response.Content=new StringContent(“注销成功您已成功注销。
您现在可以关闭此窗口/选项卡”); response.Headers.AddCookies(cookie1);//类型不匹配 返回响应; }

如何实现我的WebAPI已注销,并且在登录之前需要再次执行OAuth?

最简单的方法是让客户端本身“忘记”令牌-无需告诉服务器它的情况(这就是清除身份验证cookie的真正目的-使浏览器删除cookie)


如果希望令牌本身不再有效,则需要维护已撤销令牌的列表。出于各种原因,您可能希望您的访问令牌始终有效,但寿命较短,并取消刷新令牌。

您无法注销API,因为您没有登录到它

例如,假设您的API使用Facebook作为其OpenID身份验证提供商。 您的用户必须登录facebook才能使用您的API。你的API会将他们重定向到facebook身份验证服务器,如果他们没有登录,facebook会要求他们登录

如果用户决定保持登录到facebook,那么每次他们使用您的API时,他们将不需要再次登录到facebook,您的中间件代码将获得一个有效令牌,以便他们访问您的API

您的API无法删除facebook和用户浏览器之间的浏览器cookie,因此您无法将他们从facebook注销,因此您无法阻止他们在需要时获得新令牌

我不知道您使用什么OpenID提供程序,但我认为上述内容适用于任何情况

您可以注销MVC应用程序,因为当您登录时,它会在您(用户代理)和MVC应用程序之间创建cookie。它可以删除自己的cookie

    [HttpGet]
    public HttpResponseMessage Logout()
    {
        FormsAuthentication.SignOut();

        // clear authentication cookie
        HttpCookie cookie1 = new HttpCookie(FormsAuthentication.FormsCookieName, "");
        cookie1.Expires = DateTime.Now.AddYears(-1);

        var response = Request.CreateResponse(HttpStatusCode.OK);
        response.Content = new StringContent("<html><title>Logout successful</title><body style=\"font-family:sans-serif\"><div style=\"display:table; width:100%; height:100%; margin:0; padding:0; \"><div style=\"display:table-cell; vertical-align:middle; text-align:center;\">You have been successfully logged out.<br>You can close this window/tab now.</div></div></body></html>");
        response.Headers.AddCookies(cookie1); // Types don't match
        return response;
    }