This commit is contained in:
j4587698
2020-02-09 19:10:05 +08:00
commit 358617b0c3
414 changed files with 96012 additions and 0 deletions

View File

@@ -0,0 +1,38 @@
using System;
using System.Linq;
using System.Reflection;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
namespace JXCMS.Core.Auth
{
public static class AuthExtension
{
public static IServiceCollection AddJXAuth(this IServiceCollection services)
{
var types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(x =>
x.GetTypes().Where(y => y.BaseType == typeof(BaseAuthorizeAttribute)));
var builder = services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme);
foreach (var type in types)
{
builder.AddCookie(type.Name.Replace("AuthorizeAttribute", ""), o =>
{
o.LoginPath = type.GetField("LoginPath", BindingFlags.Static|BindingFlags.Public).GetValue(type) as string;
o.AccessDeniedPath =
type.GetField("AccessDeniedPath", BindingFlags.Static | BindingFlags.Public).GetValue(type) as string;
});
}
return services;
}
public static async Task LoginAsync(BaseAuthorizeAttribute authorize, HttpContext httpContext, ClaimsPrincipal claims)
{
await httpContext.SignInAsync(authorize.GetScheme(), claims);
}
}
}

View File

@@ -0,0 +1,21 @@
using Microsoft.AspNetCore.Authorization;
namespace JXCMS.Core.Auth
{
public abstract class BaseAuthorizeAttribute : AuthorizeAttribute
{
public static string LoginPath = "/Admin/Login";
public static string AccessDeniedPath = "/Error/Forbidden";
public BaseAuthorizeAttribute()
{
AuthenticationSchemes = GetScheme();
}
public string GetScheme()
{
return GetType().Name.Replace("AuthorizeAttribute", "");
}
}
}