Use with JWT #3081
Replies: 2 comments 6 replies
-
No, I don't think we have a demo of using JWTs as part of CSLA at the moment. Creating a demo is a bit problematic for reasons I will come to, and not entirely relevant. CSLA tries not to involve itself with authentication, wherever possible. Instead, it delegates that responsibility to the platform, as far as it can. Using JWTs with CSLA should be exactly the same as using it with any other client/server pair of the technologies you are using. That's why I say it's not entirely relevant. It's not completely irrelevant, but the point is that it doesn't really have anything to do with CSLA. There are a couple of sticking point when it comes to creating a demo of this usage:
This may not seem like a good start to helping you, but it's relevant I explain why such a demo doesn't exist - yet. Adding one was discussed on the Discord channel recently, and someone offered to add one if they get time, so hopefully there will be one in the future. Now let me try to help explain how a system using JWTs will work. You haven't given much detail of the version of .NET you are intending to use, so I will assume that it is .NET 6 in this answer. If that's not the case then the answer may be wrong. It looks like you are talking about using the HTTP remote data portal. On the server side in ASP.NET Core, there is built in support for JWTs. The implementation is achieved using middleware. Enabling this is a case of configuring auth to support the particular auth provider you want to use. Sadly that varies a little bit by provider, but Google an example of the one you are interested in. On the client side, you need to add the JWT to each request, and I think that might be what you are asking about. CSLA uses HttpClient under the covers for the HTTP remote data portal [there is a caveat later.] HttpClient requests can be intercepted using custom message handlers. This is the client-side equivalent of middleware. Again, this is a generic solution for all use of JWTs over HTTP, and the use of CSLA doesn't change how it is done. Any code you can find as an example should work. Here's an overview doc, but there are plenty of other samples out there. https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/httpclient-message-handlers The caveat is relevant to client code; be careful of this. HttpClient only offers async methods - it's an intentional limitation of the implementation that Microsoft built for .NET Core. That means you HAVE to use async method calls from the client for HttpClient to be used. If you use synchronous data access method calls then CSLA falls back to using WebClient, and that doesn't respect message handlers as far as I know. In short, you need to use IDataPortal.CreateAsync, IDataPortal.FetchAsync, and SaveAsync on your business object on the client. If you do that, the request is achieved using HttpClient, and the message handler will enable interception to add the JWT. |
Beta Was this translation helpful? Give feedback.
-
Thank you very much for the detailed explanation, you totally guessed my idea, there is no problem putting JWT TOKEN into HttpClient request header, I focus on how to intercept the request and request the latest TOKEN from the server before the service is cleared, if the TOKEN is about to expire. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
If the project has multiple sides, JWT is a better authentication method. Is there any event or interface related to the HTTP interface of CSLA? Before processing the service invocation of the server, JWT-related requests should be made, such as determining whether the TOKEN should expire and refreshing to obtain the latest TOKEN in time? There is a related DEMO for reference no.
Beta Was this translation helpful? Give feedback.
All reactions