Lightweight authorization framework
Cyaim.Authentication:authorization common library.
NebulaAuthServer.AccessManagement:All standard,authorization model it's here.
NebulaAuthServer:Authorization center.
Project Nuget address:
https://www.nuget.org/packages/Cyaim.Authentication/
Nuget run command.
Install-PackageCyaim.Authentication-Version1.0.0In "Startup.cs" -> "ConfigureServices" Method,the method last add code.
services.ConfigureAuth(x =>{x.SourceLocation=ParameterLocation.Header;x.PreAccessEndPointKey="Sys";});In "Configure" Method add middleware.
AuthMiddleware must add above UseEndpoints.
app.UseAuth();In need of authorization Controller or Action mark:
[AuthEndPoint()]In not authorization Action mark:
[AuthEndPoint(allowGuest:true)]Here operation PostgreSQL and redis, You can replace it with something you like.
usingSystem;usingSystem.IO;usingSystem.Linq;usingSystem.Threading.Tasks;usingNewtonsoft.Json;usingstaticCyaim.Authentication.Infrastructure.Helpers.URLStructHelper;publicclassAuth{staticAuth(){_npgsqlDapperHelper=NpgsqlDapperHelper.Helper("Your databse connection string");_redisHelper=newRedisHelper("Your redis connection string","Auth",0);}privatereadonlystaticNpgsqlDapperHelper_npgsqlDapperHelper;privatereadonlystaticRedisHelper_redisHelper;conststringROUTE="/api/v1/{controller}/{action}";publicstaticasyncTask<AuthEndPointAttribute[]>GetAuthEndPointByUser(stringauthKey,HttpContexthttpContext,AuthOptionsauthOptions){stringpersonId=GetUserIdByRedis(authKey);if(personId==null){// to do request is not loginpersonId="sys_guest";}URLStructurlStruct=GetUrlStruct(ROUTE,httpContext.Request.Path);// Adopt request URL search watching endpointvarwatchep=authOptions.WatchAuthEndPoint.FirstOrDefault(x =>x.Routes!=null&&x.ControllerName?.ToLower()==urlStruct.Controller.ToLower()+"controller"&&x.Routes.Any(r =>r.Template?.ToLower()==urlStruct.Action?.ToLower()));if(watchep==null){//This means that the request is not listening//goto GoNonAccess;Console.WriteLine($@"Endpoint -> {httpContext.Request.Path} not databse watching range.");gotoNonAccessWatch;}//----------Begin database query user authorization code----------if(user permission ==false){gotoGoNonAccess;}//----------End your code----------returnnewAuthEndPointAttribute[1]{watchep};//Non AccessGoNonAccess:returnnewAuthEndPointAttribute[0];//Non watching rangeNonAccessWatch:returnnull;}/// <summary>/// From redis get token/// </summary>/// <param name="authKey"></param>publicstaticstringGetUserIdByRedis(stringauthKey){varpersonId=_redisHelper.StringGet(authKey);returnpersonId;}}In "Startup.cs" -> "ConfigureServices" Method,the method last replace code.
services.ConfigureAuth(x =>{x.SourceLocation=ParameterLocation.Header;x.ExtractDatabaseAuthEndPoints=newAuthOptions.ExtractAuthEndPointsHandler(Auth.GetAuthEndPointByUser);x.PreAccessEndPointKey="Sys";});