72 lines
3.3 KiB
C#
72 lines
3.3 KiB
C#
using CuentasCobrar.UI.Services.Interfaces;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System.IdentityModel.Tokens.Jwt;
|
|
using System.Security.Claims;
|
|
using static CuentasCobrar.UI.Helpers.Enum;
|
|
|
|
namespace CuentasCobrar.UI.Helpers
|
|
{
|
|
public class BaseController : Controller
|
|
{
|
|
string pos = "";
|
|
public void BasicNotification ( string msj, TipoNotificacion type, string title = "" )
|
|
{
|
|
TempData["notification"] = $"Swal.fire('{title}','{msj}', '{type.ToString ().ToLower ()}', 'Prueba')";
|
|
}
|
|
|
|
// el parametro de timer con valor 0 se deshabilita
|
|
public void CustomNotification ( string msj, TipoNotificacion type, NotificationPosition position, string title = "", bool showConfirmButton = false, int timer = 1500, bool toast = false )
|
|
{
|
|
SetPosition ( position.ToString () );
|
|
|
|
TempData["notification"] = "Swal.fire({customClass:{confirmButton:'btn btn-primary',cancelButton:'btn btn-danger'},position:'" + pos + "',type:'" + type.ToString ().ToLower () +
|
|
"',icon:'" + type.ToString ().ToLower () + "',title:'" + title + "',text: '" + msj + "',showConfirmButton: " + showConfirmButton.ToString ().ToLower () + ",confirmButtonColor: '#4F0DA2',toast: "
|
|
+ toast.ToString ().ToLower () + ",timer: " + timer + "}); ";
|
|
}
|
|
|
|
public async Task<string> CrearToken ( IUsuarioAPI usuarioAPI )
|
|
{
|
|
var correoUsuario = User.Identity?.Name;
|
|
string tokenUsuario = await usuarioAPI.CrearToken ( correoUsuario );
|
|
return tokenUsuario;
|
|
}
|
|
|
|
public void CrearCookies ( string tokenUsuario )
|
|
{
|
|
string cookieToken = "CookieToken";
|
|
string cookieRoles = "CookieRoles";
|
|
var tokenObjeto = new JwtSecurityTokenHandler ().ReadJwtToken ( tokenUsuario );
|
|
var identity = new ClaimsIdentity ( tokenObjeto.Claims );
|
|
var rolesClaims = identity.FindAll ( ClaimTypes.Role ).ToList ();
|
|
List<string> roles = new List<string> ();
|
|
foreach (var item in rolesClaims)
|
|
{
|
|
roles.Add ( item.Value );
|
|
}
|
|
var cookieConfig = new CookieOptions ();
|
|
cookieConfig.Expires = DateTime.Now.AddMinutes ( 10 );
|
|
cookieConfig.Path = "/";
|
|
cookieConfig.Secure = true;
|
|
cookieConfig.HttpOnly = true;
|
|
cookieConfig.SameSite = SameSiteMode.Lax;
|
|
Response.Cookies.Append ( cookieToken, tokenUsuario, cookieConfig );
|
|
cookieConfig.Expires = DateTime.Now.AddDays ( 5 );
|
|
Response.Cookies.Append ( cookieRoles, String.Join ( ",", roles ), cookieConfig );
|
|
}
|
|
|
|
#region Methods
|
|
private void SetPosition ( string position )
|
|
{
|
|
if (position == "Top") pos = "top";
|
|
if (position == "TopStart") pos = "top-start";
|
|
if (position == "TopEnd") pos = "top-end";
|
|
if (position == "Center") pos = "center";
|
|
if (position == "CenterStart") pos = "center-start";
|
|
if (position == "CenterEnd") pos = "center-end";
|
|
if (position == "Bottom") pos = "bottom";
|
|
if (position == "BottomStart") pos = "bottom-start";
|
|
if (position == "BottomEnd") pos = "bottom-end";
|
|
}
|
|
#endregion
|
|
}
|
|
}
|