MVCLogin/VCart/Controllers/LoginController.cs
2024-09-19 11:43:12 -03:00

112 lines
3.8 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Identity;
using System.Security.Claims;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication;
using Stripe;
namespace VCart.Controllers
{
public class LoginController : Controller
{
private readonly ILogger<LoginController> logger;
private readonly IHttpClientFactory httpClientFactory;
private readonly StripeClient _stripeClient;
public LoginController(ILogger<LoginController> logger, IHttpClientFactory httpClientFactory)
{
this.logger = logger;
this.httpClientFactory = httpClientFactory;
}
public IActionResult Index()
{
return View("~/Views/Login/Index.cshtml");
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult ExternalLogin(string provider, string returnUrl)
{
var redirectUrl = Url.Action("ExternalLoginCallback", "Login", new { ReturnUrl = returnUrl });
var properties = new AuthenticationProperties { RedirectUri = "https://localhost:44377" + redirectUrl };
return Challenge(properties, "Microsoft");
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult ExternalLoginGoogle(string provider, string returnUrl)
{
var redirectUrl = Url.Action("ExternalLoginCallback", "Login", new { ReturnUrl = returnUrl });
var properties = new AuthenticationProperties { RedirectUri = "https://localhost:44377" + redirectUrl };
return Challenge(properties, "Google");
}
[AllowAnonymous]
[HttpGet]
public async Task<ActionResult> ExternalLoginCallback(string code="")
{
//TODO: Temporário
var emailExist = HttpContext.User.FindFirst(ClaimTypes.Email).Value;
if (emailExist != null)
{
var claims = new List<Claim>
{
new Claim(ClaimTypes.Name, emailExist),
new Claim("FirstName", HttpContext.User.FindFirst(ClaimTypes.GivenName).Value),
new Claim("FullName", HttpContext.User.FindFirst(ClaimTypes.GivenName).Value + " " + HttpContext.User.FindFirst(ClaimTypes.Surname).Value),
new Claim(ClaimTypes.Role, "User"),
};
var claimsIdentity = new ClaimsIdentity(
claims,
CookieAuthenticationDefaults.AuthenticationScheme
);
var authProperties = new AuthenticationProperties
{
//AllowRefresh = <bool>,
// Refreshing the authentication session should be allowed.
//ExpiresUtc = DateTimeOffset.UtcNow.AddMinutes(10),
// The time at which the authentication ticket expires. A
// value set here overrides the ExpireTimeSpan option of
// CookieAuthenticationOptions set with AddCookie.
//IsPersistent = true,
// Whether the authentication session is persisted across
// multiple requests. When used with cookies, controls
// whether the cookie's lifetime is absolute (matching the
// lifetime of the authentication ticket) or session-based.
//IssuedUtc = <DateTimeOffset>,
// The time at which the authentication ticket was issued.
//RedirectUri = <string>
// The full path or absolute URI to be used as an http
// redirect response value.
};
await HttpContext.SignInAsync(
CookieAuthenticationDefaults.AuthenticationScheme,
new ClaimsPrincipal(claimsIdentity),
authProperties);
return RedirectToAction("Index", "Startup");
}
ViewBag.ErrorTitle = $"Email claim not received from: Microsoft";
ViewBag.ErrorMessage = "Please contact support on info@dotnettutorials.net";
return View("Error");
}
[HttpGet]
public async Task<IActionResult> Logout()
{
await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
return RedirectToAction("Index", "Home");
}
}
}