Ale*_*der 1 c# cookies oauth-2.0 asp.net-web-api
我有一个带有 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);
}
}
});
Run Code Online (Sandbox Code Playgroud)
并强制所有控制器使用登录
config.Filters.Add(new System.Web.Http.AuthorizeAttribute());
Run Code Online (Sandbox Code Playgroud)
我现在想添加一个名为 LogoutController 的 ApiController(猜猜它是做什么的)。
我发现我可以使用以下方式从 MVC 注销
System.Web.Security.FormsAuthentication.SignOut();
Run Code Online (Sandbox Code Playgroud)
但我没有以这种方式从 WebAPI 注销。我没有找到任何如何从 WebAPI 注销的信息。但我发现注销过程中可能存在错误,cookie 被保留并必须手动删除,但是代码又是 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;
}
Run Code Online (Sandbox Code Playgroud)
如何实现我的 WebAPI 已注销并且需要在登录之前再次完成 OAuth?
最简单的方法是客户端本身“忘记”令牌 - 无需告诉服务器(这就是清除身份验证 cookie 的真正作用 - 让浏览器删除 cookie)。
如果您希望令牌本身不再有效,则需要维护已撤销令牌的列表。由于各种原因,您可能希望访问令牌始终有效但寿命短暂,并改为撤销刷新令牌。
| 归档时间: |
|
| 查看次数: |
6533 次 |
| 最近记录: |