-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathAccountController.cs
More file actions
43 lines (40 loc) · 1.78 KB
/
AccountController.cs
File metadata and controls
43 lines (40 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using DevExpress.ExpressApp.Security;
using Microsoft.AspNetCore.Antiforgery;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OData.Routing.Controllers;
using System.Security.Claims;
namespace DevExtreme.OData.Controllers {
public class AccountController : ODataController {
private readonly SignInManager signInManager;
private readonly IAntiforgery antiforgery;
public AccountController(SignInManager signInManager, IAntiforgery antiforgery) {
this.signInManager = signInManager;
this.antiforgery = antiforgery;
}
[HttpPost("Login")]
[AllowAnonymous]
public async Task<ActionResult> Login(string userName, string password) {
Response.Cookies.Append("userName", userName ?? string.Empty);
var authenticationResult = signInManager.AuthenticateByLogonParameters(new AuthenticationStandardLogonParameters(userName, password));
if(authenticationResult.Succeeded) {
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, authenticationResult.Principal);
try {
HttpContext.User = authenticationResult.Principal;
var tokens = antiforgery.GetAndStoreTokens(HttpContext);
return Ok(tokens.RequestToken);
} finally {
HttpContext.User = new ClaimsPrincipal();
}
}
return Unauthorized();
}
[HttpGet("Logout")]
public async Task<ActionResult> Logout() {
await HttpContext.SignOutAsync();
return Ok();
}
}
}