movies
This commit is contained in:
38
Core/JXCMS.Core/Auth/AuthExtension.cs
Executable file
38
Core/JXCMS.Core/Auth/AuthExtension.cs
Executable 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
21
Core/JXCMS.Core/Auth/BaseAuthorizeAttribute.cs
Executable file
21
Core/JXCMS.Core/Auth/BaseAuthorizeAttribute.cs
Executable 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", "");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user