Comments (4)
Thank you! Can you send a PR?
from moon.aspnetcore.
I'd need a day, unless: is there a way to submit a PR directly without forking?
from moon.aspnetcore.
To be honest, I'm hesitant because of the lack of tests on the project. This seems like an innocuous change, but with the level of "magic" in aspnet/Security I'd be worried about breaking something else without some regression testing.
I could probably at least create another "sample" project that exercises the feature. If you're okay with reviewing the change against that and the existing example, then that's okay with me.
from moon.aspnetcore.
There is a very "Ugly" hack to get this working.
public static class AuthenticationServices
{
public static IServiceCollection AddAuthenticationServices(this IServiceCollection services)
{
if (services == null)
return null;
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddTransient<IPrincipalProvider, HttpContextPrincipalProvider>();
services.AddTransient<Authenticator>();
var provider = services.BuildServiceProvider();
services.AddAuthentication("Basic")
.AddBasic(o =>
{
o.Realm = "Sphinx";
o.Events = provider.CreateScope().ServiceProvider.GetRequiredService<Authenticator>();
});
return services;
}
}
public class Authenticator : BasicAuthenticationEvents
{
public IRepository Repository { get; }
public IMemoryCache MemoryCache { get; }
public Authenticator(IRepository repository) {
this.Repository = repository ?? throw new ArgumentNullException(nameof(repository));
}
public override async Task SignInAsync(BasicSignInContext context) {
//Check Cache
var cacheKey = Extensions.CacheExtensions.GenerateCacheKey(context.UserName, context.Password);
var data = await this.CreateClaimsFor(context);
if(data == null)
return;
var identity = new ClaimsIdentity(data, context.Scheme.Name);
context.Principal = new ClaimsPrincipal(identity);
}
private async Task<Claim[]> CreateClaimsFor(BasicSignInContext context) {
if (!context.UserName.Equals("apikey", StringComparison.InvariantCultureIgnoreCase))
return null;
var intergration = await this.Repository.GetByApiKeyAsync(context.Password);
if (intergration == null || intergration.Status != Constants.Integration.Status.Active)
return null;
if (intergration.ApiKey != context.Password)
return null;
var claims = new[] {
new Claim("sub", intergration.Uuid.ToString("N")),
new Claim("name", intergration.Name),
new Claim("status", intergration.Status),
new Claim("organisation", intergration.Organisation.Uuid.ToString("N"))
};
return claims;
}
}
from moon.aspnetcore.
Related Issues (3)
- AuthenticateResult.Success cannot have null ticket
- upgrade 2.1 to 3.1 - Could not load type 'Microsoft.AspNetCore.Mvc.DataAnnotations.Internal.NumericClientModelValidatorProvider' from assembly 'Microsoft.AspNetCore.Mvc.DataAnnotations, Version=3.1.32.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from moon.aspnetcore.