aspnetcorehero / abstractions Goto Github PK
View Code? Open in Web Editor NEWCollection of Abstract Classes to improve code reusability within projects.
Collection of Abstract Classes to improve code reusability within projects.
Can you add some baseControllers and extensions
public class BaseController<T> : Controller where T : Controller
{
private IBreadCrumbService _breadCrumbInstance;
private INotyfService _toastInstance;
private ILogger _loggerInstance;
protected INotyfService _toast => _toastInstance ??= HttpContext.RequestServices.GetService<INotyfService>();
protected IBreadCrumbService _breadcrumbs =>
_breadCrumbInstance ??= HttpContext.RequestServices.GetService<IBreadCrumbService>();
protected ILogger _logger => _loggerInstance ??= HttpContext.RequestServices.GetService<ILogger<T>>();
}
Claims
using System.Security.Claims;
namespace Infrastructure.Extensions;
public static class ClaimsPrincipalExtensions
{
/// <summary>
/// Returns the principal user email claim value
/// </summary>
/// <param name="principal"></param>
/// <returns></returns>
public static string GetUserEmail(this ClaimsPrincipal principal)
{
return principal.FindFirstValue(ClaimTypes.Email) ?? string.Empty;
}
/// <summary>
/// Returns the principal user id identifier claim value
/// </summary>
/// <param name="principal"></param>
/// <returns></returns>
public static string GetUserId(this ClaimsPrincipal principal)
{
return principal.FindFirstValue(ClaimTypes.NameIdentifier) ?? string.Empty;
}
/// <summary>
/// Returns the principal user username
/// </summary>
/// <param name="principal"></param>
/// <returns></returns>
public static string GetUsername(this ClaimsPrincipal principal)
{
return principal.FindFirstValue(ClaimTypes.Name) ?? string.Empty;
}
public static string FirstName(this ClaimsPrincipal principal)
{
return principal.FindFirstValue(ClaimTypes.GivenName) ?? string.Empty;
}
public static string LastName(this ClaimsPrincipal principal)
{
return principal.FindFirstValue(ClaimTypes.Surname) ?? string.Empty;
}
/// <summary>
/// Checks to see if the user given by id matches the principal user id
/// </summary>
/// <param name="principal"></param>
/// <param name="id"></param>
/// <returns></returns>
public static bool IsCurrentUser(this ClaimsPrincipal principal, string id)
{
var currentUserId = GetUserId(principal);
return string.Equals(currentUserId, id, StringComparison.OrdinalIgnoreCase);
}
/// <summary>
/// Returns the roles held by principal
/// </summary>
/// <param name="principal"></param>
/// <returns></returns>
public static IEnumerable<string> GetRoles(this ClaimsPrincipal principal)
{
return principal.Identities.SelectMany(i =>
{
return i.Claims
.Where(c => c.Type == i.RoleClaimType)
.Select(c => c.Value)
.ToList();
});
}
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.