Skip to content
This repository was archived by the owner on Mar 17, 2023. It is now read-only.

Commit 0fd52ef

Browse files
committed
fix logout
1 parent f54fa55 commit 0fd52ef

File tree

2 files changed

+31
-16
lines changed

2 files changed

+31
-16
lines changed
Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,37 @@
11
using Microsoft.AspNetCore.Authentication;
22
using Microsoft.AspNetCore.Authentication.Cookies;
3-
using Microsoft.AspNetCore.Authentication.Twitter;
3+
using Microsoft.AspNetCore.Authentication.Twitter;
44
using Microsoft.AspNetCore.Authorization;
55
using Microsoft.AspNetCore.Mvc;
6-
using Microsoft.Extensions.Logging;
6+
using Microsoft.Extensions.Logging;
77
using System.Collections.Generic;
88
using System.Security.Claims;
99
using System.Threading.Tasks;
10-
10+
1111
namespace RPSLS.Web.Controllers
1212
{
1313
[AllowAnonymous]
1414
[Route("api/[controller]")]
1515
public class AccountController : Controller
1616
{
17-
private readonly ILogger<AccountController> _logger;
18-
private const string REDIRECT_URI = "/";
19-
20-
public AccountController(ILogger<AccountController> logger)
21-
{
22-
_logger = logger;
17+
private readonly ILogger<AccountController> _logger;
18+
private const string REDIRECT_URI = "/";
19+
20+
public AccountController(ILogger<AccountController> logger)
21+
{
22+
_logger = logger;
2323
}
2424

2525
[HttpGet("login/twitter")]
26-
public IActionResult ExternalLogin([FromQuery]string redirectUrl)
27-
{
28-
var properties = new AuthenticationProperties { RedirectUri = redirectUrl ?? REDIRECT_URI };
29-
_logger.LogInformation($"Twitter login redirected to {properties.RedirectUri}");
30-
return base.Challenge(properties, TwitterDefaults.AuthenticationScheme);
26+
public IActionResult ExternalLogin([FromQuery] string redirectUrl)
27+
{
28+
var properties = new AuthenticationProperties { RedirectUri = redirectUrl ?? REDIRECT_URI };
29+
_logger.LogInformation($"Twitter login redirected to {properties.RedirectUri}");
30+
return base.Challenge(properties, TwitterDefaults.AuthenticationScheme);
3131
}
3232

3333
[HttpGet("login")]
34-
public async Task<IActionResult> Login([FromQuery]string username, [FromQuery]string redirectUrl)
34+
public async Task<IActionResult> Login([FromQuery] string username, [FromQuery] string redirectUrl)
3535
{
3636
var claims = new List<Claim> {
3737
new Claim(ClaimTypes.Name, username)
@@ -43,5 +43,20 @@ public async Task<IActionResult> Login([FromQuery]string username, [FromQuery]st
4343
_logger.LogInformation($"Cookies login redirected to {redirectUrl ?? REDIRECT_URI}");
4444
return Redirect(redirectUrl ?? REDIRECT_URI);
4545
}
46+
47+
[HttpGet("logout")]
48+
public async Task<IActionResult> Logout()
49+
{
50+
if (HttpContext.User.Identity.AuthenticationType == TwitterDefaults.AuthenticationScheme)
51+
{
52+
await HttpContext.SignOutAsync(TwitterDefaults.AuthenticationScheme);
53+
}
54+
else
55+
{
56+
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
57+
}
58+
_logger.LogInformation($"Logout Completed");
59+
return Redirect(REDIRECT_URI);
60+
}
4661
}
4762
}

Source/Services/RPSLS.Web/Pages/Index.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,6 @@
138138
IsTwitterUser = false;
139139
}
140140

141-
NavigationManager.NavigateTo("/", true);
141+
NavigationManager.NavigateTo("/api/account/logout", true);
142142
}
143143
}

0 commit comments

Comments
 (0)