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; using ChatMvc.Managers; namespace ChatMvc.Controllers { public class LoginController : Controller { private readonly ILogger logger; private readonly IHttpClientFactory httpClientFactory; private readonly TokenManager tokenManager; private readonly StripeClient _stripeClient; public LoginController(ILogger logger, IHttpClientFactory httpClientFactory, TokenManager tokenManager) { this.logger = logger; this.httpClientFactory = httpClientFactory; this.tokenManager = tokenManager; } 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:7078" + 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:7078" + 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) { List claims = null; if (HttpContext.User.FindFirst(ClaimTypes.GivenName) != null) { var token = await tokenManager.GetToken(emailExist, "Domvs iT", HttpContext.User.FindFirst(ClaimTypes.GivenName).Value); claims = new List { new Claim(ClaimTypes.Email, 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("CompanyName", "Domvs iT"), new Claim("UserId", emailExist), new Claim("TokenExternal", token), new Claim(ClaimTypes.Role, "User"), }; } else if (HttpContext.User.FindFirst(ClaimTypes.Name)!=null) { var name = HttpContext.User.FindFirst(ClaimTypes.Name).Value; var firstName = name.Split(' ')[0]; var token = await tokenManager.GetToken(emailExist, "Domvs iT", firstName); claims = new List { new Claim(ClaimTypes.Email, emailExist), new Claim("FirstName", firstName), new Claim("FullName", name), new Claim("UserId", emailExist), new Claim("CompanyName", "Domvs iT"), new Claim("TokenExternal", token), 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", "Chat"); } 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"); } } }