✅ How do you create custom authz/autho in ASP.NET Core?
As far as I know, on the internet there's only an implementation for JWT authentication. Yet, I need totally custom implementation that's not related to JWT at all.
I have an additional case that every static file visit should also be authenticated (the specification said so).
According to my implementation, anonymous access should be allowed. But for unknown reasons, I got empty content with 401 status code for any page and any method.
I think I'm missing something, yet I don't know what it is.
8 Replies
Currently my code is:
Program.cs
SessionAuthenticationHandler.cs
SessionAuthenticationMiddleware.cs
SessionAuthentication.cs
you're on the right track with implementing your own
AuthenticationHandler
class
what's different about this approach than the ready-made Session auth scheme?
I.E. Cookies
you definitely do not need to be implementing your own middlewareWas this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.I don't understand why I didn't need to implement my own middleware. Since for my current case, anonymous access should be possible for now.
Or.. should ai rename my scheme to anything other than "Session"?
what does creating custom middleware have to do with allowing anonymous access?
why do you think that anonymous access is only possible if you create custom middleware?
I got where you confused at. I'm creating allow anonymous access inside the middleware only for development purposes.
Yes I know that I can just toggle app.Configuration.Environment condition, but this is something I'd like to try (if this is possible)
So assuming I don't need to change my scheme name to anything else and just do authenticated access all the time, and you've said I'm on the right track.. then what other things that I need to know moving on?
if that's what you want to go with, you should
A) name your middleware more appropriately, E.G.
DevelopmentAuthorizationMiddleware
, and then make it actually do what you describe, cause right now doesn't allow anonymous access for anything
B) inject it like this:
more likely, you can find a way to achieve this through just policies, or just.... don't do this at all
if you just want to avoid having to sign in all the time in DVLP, there's ways to do that through config and toolingThank you so much!