1
1
using Microsoft . AspNetCore . Authentication ;
2
2
using Microsoft . AspNetCore . Authentication . Cookies ;
3
- using Microsoft . AspNetCore . Authentication . Twitter ;
3
+ using Microsoft . AspNetCore . Authentication . Twitter ;
4
4
using Microsoft . AspNetCore . Authorization ;
5
5
using Microsoft . AspNetCore . Mvc ;
6
- using Microsoft . Extensions . Logging ;
6
+ using Microsoft . Extensions . Logging ;
7
7
using System . Collections . Generic ;
8
8
using System . Security . Claims ;
9
9
using System . Threading . Tasks ;
10
-
10
+
11
11
namespace RPSLS . Web . Controllers
12
12
{
13
13
[ AllowAnonymous ]
14
14
[ Route ( "api/[controller]" ) ]
15
15
public class AccountController : Controller
16
16
{
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 ;
23
23
}
24
24
25
25
[ 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 ) ;
31
31
}
32
32
33
33
[ 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 )
35
35
{
36
36
var claims = new List < Claim > {
37
37
new Claim ( ClaimTypes . Name , username )
@@ -43,5 +43,20 @@ public async Task<IActionResult> Login([FromQuery]string username, [FromQuery]st
43
43
_logger . LogInformation ( $ "Cookies login redirected to { redirectUrl ?? REDIRECT_URI } ") ;
44
44
return Redirect ( redirectUrl ?? REDIRECT_URI ) ;
45
45
}
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
+ }
46
61
}
47
62
}
0 commit comments