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 Postall.Controllers { public class LoginController : Controller { private readonly ILogger logger; private readonly IHttpClientFactory httpClientFactory; private readonly StripeClient _stripeClient; public LoginController(ILogger 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 = "http://localhost:5094" + redirectUrl }; return Challenge(properties, "Microsoft"); } [HttpPost] [ValidateAntiForgeryToken] public ActionResult ExternalLoginGoogle(string provider, string returnUrl) { var redirectUrl = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host}{Url.Action("ExternalLoginCallback", "Login")}"; //var properties = new AuthenticationProperties { RedirectUri = "https://localhost:7078" + redirectUrl }; var properties = new AuthenticationProperties { RedirectUri = redirectUrl }; return Challenge(properties, "Google"); } [AllowAnonymous] [HttpGet] public async Task ExternalLoginCallback(string code="") { //TODO: Temporário var emailExist = HttpContext.User.FindFirst(ClaimTypes.Email).Value; if (emailExist != null) { var claims = new List { 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 = , // 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 = , // The time at which the authentication ticket was issued. //RedirectUri = // 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 Logout() { await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme); return RedirectToAction("Index", "Home"); } } }