Home > Enterprise >  Login via API (outside app) and create a cookie for access
Login via API (outside app) and create a cookie for access

Time:05-16

Ok so I think I've settled on choosing BLAZOR for my upcoming project. But first I need to do something that is seemingly very basic.

For context, Blazor Server side will be how I interface with my SQL Server dB, I want "individual accounts" mode to be the way users authenticate. I'm using net 6.0,almost all tutorials out there seem to be net 5 since they all still have startup.cs files. Ok, but I also am creating a parallel app that is NOT a website, but I want it to grab data from the same database via routes after authenticating.

For example, website.com/api/data?variablestograb as a GET would send me some json data.

OK that being said, how do I login programmatically from an outside app? If you must know, the outside app is part of Unity C#. But that doesn't matter so much, what itll do is use a post call to login in via the api routes. Something like

Website.com/api/login?un=blah&pw=haha

This will generate a cookie and I can grab it with the session data and I'll use this with every get call hence.

Just using the basic templates, Blazor server net 6.0 with individual auth, how do I set up such a route? Looking at the files, I'm at a complete loss on how the login pages are actually passing data around.

Thanks!

Update: the specific ask is exactly how do I modify the Blazor Server Net 6 Individual Accounts template to allow me to authenticate a user via an external access api? My thought would be to reference the route above for /login/ but that might not even be the best practice. But even if it is, how exactly and where would I update the template to make this happen?

(I have a feeling it's pretty basic to do, but I've been reading tutorials for weeks now and they're all just talking about internal authentication and verification within each Blazor component. I basically want an external program to also be able to "drive the car" remotely, but first it must authenticate.)

CodePudding user response:

If you're building an API for this from scratch, then it seems like you have the right idea, no matter what happens, you're going to send the cookie to be website every request or at least a session id which contains all the information provided. From a database perspective maybe create a session table which then contains all the info you want and also can be easily found. That's a way for you to create a cookie for client server communication, however this is from my limited knowledge and may not be the best way.

I'd recommend hiding information like keys in the header to prevent exposure, looking at other APIs like the Spotify API you can see they set the authorisation bearer.

Exposing all the information for the credentials in the URL for what could be sensitive database behaviour may not be the best. You can hide the information in the header for every request you make.

Maybe have a default controller that obtains the user information before handling any specific requests and making it accessible to your other methods/requests?

CodePudding user response:

The basic process for any external authentication is:

  1. Redirect to the external log in page.
  2. External Provider does it business.
  3. External provider posts to a page on your site with the authentication information included - normally security info in the header or a cookie.
  4. The Blazor app reads the authentication information with the AuthenticationStateProvider.

Normally you just need to write a customer AuthenticationStateProvider and code to redirect if the user is not authorized. This may be a manual button in the top bar, a you aren't logged in page with a button to log in, or an automatic redirect to the provider. The return for the provider is either your landing page or some other page to tell them they logged in successfully.

The custom AuthenticationStateProvider replaces the standard one in DI services and provides the security information to the Authorization components.

Search for "blazor custom authentication provider" will get you lots of good resources.

  • Related