102 lines
3.2 KiB
C#
102 lines
3.2 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;
|
|
|
|
namespace VCart.Controllers
|
|
{
|
|
public class LoginController : Controller
|
|
{
|
|
private readonly ILogger<LoginController> logger;
|
|
private readonly IHttpClientFactory httpClientFactory;
|
|
|
|
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");
|
|
}
|
|
|
|
[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("FullName", HttpContext.User.FindFirst(ClaimTypes.GivenName).Value),
|
|
new Claim(ClaimTypes.Role, "Administrator"),
|
|
};
|
|
|
|
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", "Home");
|
|
}
|
|
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");
|
|
}
|
|
}
|
|
}
|