【发布时间】:2016-10-11 02:31:36
【问题描述】:
我有一个配置如下 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 的 ApiController(猜猜它的作用)。
I have found that I can logout from MVC 使用
System.Web.Security.FormsAuthentication.SignOut();
但我没有以这种方式从 WebAPI 注销。我还没有找到任何关于如何从 WebAPI 注销的信息。但是我发现there may be a bug in logout procedure, the cookie is kept and has to be removed manually,但是代码又是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;
}
如何实现我的 WebAPI 已注销并且在我登录之前是否需要再次完成 OAuth?
【问题讨论】:
标签: c# cookies asp.net-web-api oauth-2.0