裕隆城折扣合作@中興低碳 折扣報表
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
1.4KB

  1. using System.Security.Claims;
  2. using LaneFlowReport.Auth;
  3. using Microsoft.AspNetCore.Authentication;
  4. using Microsoft.AspNetCore.Authentication.Cookies;
  5. using Microsoft.AspNetCore.Mvc;
  6. namespace LaneFlowReport.Controllers {
  7. public class LoginController: Controller {
  8. private readonly ParkingAuthProvider _parkingAuthProvider;
  9. public LoginController(ParkingAuthProvider parkingAuthProvider) {
  10. _parkingAuthProvider = parkingAuthProvider;
  11. }
  12. [HttpGet]
  13. public async Task<IActionResult> LoginCallback(string mode, string providerKey) {
  14. var accessCode = $"{mode};{providerKey}";
  15. var result = await _parkingAuthProvider.GetUserInfo(accessCode);
  16. var claimsPrincipal = new ClaimsPrincipal(new ClaimsIdentity(new[] {
  17. new Claim(ClaimTypes.Name, result.UserName),
  18. new Claim(ClaimTypes.Role, "Report"),
  19. }, "Cookies"));
  20. var authProperties = new AuthenticationProperties {
  21. IsPersistent = true
  22. };
  23. await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme,
  24. new ClaimsPrincipal(claimsPrincipal), authProperties);
  25. HttpContext.User = claimsPrincipal;
  26. ViewBag.Status = "authing...";
  27. ViewBag.RedirectUrl = Url.Action("Index", "Report", null, HttpContext.Request.Scheme);
  28. return View();
  29. }
  30. }
  31. }