Cyaim.Authentication
1.0.0
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package Cyaim.Authentication --version 1.0.0
NuGet\Install-Package Cyaim.Authentication -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Cyaim.Authentication" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Cyaim.Authentication" Version="1.0.0" />
<PackageReference Include="Cyaim.Authentication" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Cyaim.Authentication --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Cyaim.Authentication, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Cyaim.Authentication@1.0.0
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Cyaim.Authentication&version=1.0.0
#tool nuget:?package=Cyaim.Authentication&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
AuthorizationSecurity
Base Endpoint authorization framework
Quick Start
1,Add auth middleware & configure auth service
In "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();
2,Go to controller
In need of authorization Controller or Action mark:
[AuthEndPoint()]
In not authorization Action mark:
[AuthEndPoint(allowGuest: true)]
3,Use Attribute Complate,Run you project~
Persistence and cache
1,You need a class verify authorization
Here operation PostgreSQL and redis, You can replace it with something you like.
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;
using static Cyaim.Authentication.Infrastructure.Helpers.URLStructHelper;
public class Auth
{
static Auth()
{
_npgsqlDapperHelper = NpgsqlDapperHelper.Helper("Your databse connection string");
_redisHelper = new RedisHelper("Your redis connection string", "Auth", 0);
}
private readonly static NpgsqlDapperHelper _npgsqlDapperHelper;
private readonly static RedisHelper _redisHelper;
const string ROUTE = "/api/v1/{controller}/{action}";
public static async Task<AuthEndPointAttribute[]> GetAuthEndPointByUser(string authKey, HttpContext httpContext, AuthOptions authOptions)
{
string personId = GetUserIdByRedis(authKey);
if (personId == null)
{
// to do request is not login
personId = "sys_guest";
}
URLStruct urlStruct = GetUrlStruct(ROUTE, httpContext.Request.Path);
// Adopt request URL search watching endpoint
var watchep = 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.");
goto NonAccessWatch;
}
//----------Begin database query user authorization code----------
if(user permission == false)
{
goto GoNonAccess;
}
//----------End your code----------
return new AuthEndPointAttribute[1] { watchep };
//Non Access
GoNonAccess: return new AuthEndPointAttribute[0];
//Non watching range
NonAccessWatch: return null;
}
/// <summary>
/// From redis get token
/// </summary>
/// <param name="authKey"></param>
public static string GetUserIdByRedis(string authKey)
{
var personId = _redisHelper.StringGet(authKey);
return personId;
}
}
2,Add Auth Handler
In "Startup.cs" → "ConfigureServices" Method,the method last replace code.
services.ConfigureAuth(x =>
{
x.SourceLocation = ParameterLocation.Header;
x.ExtractDatabaseAuthEndPoints = new AuthOptions.ExtractAuthEndPointsHandler(Auth.GetAuthEndPointByUser);
x.PreAccessEndPointKey = "Sys";
});
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.1
- Microsoft.AspNetCore.Mvc.Core (>= 2.2.5)
- Microsoft.Extensions.Caching.Abstractions (>= 3.1.9)
- Microsoft.Extensions.DependencyInjection (>= 3.1.8)
- Microsoft.OpenApi (>= 1.2.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.