Click here to Skip to main content
15,884,838 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hello everybody,

I have a WEBAPI REST Service (which is kind of a middle layer) wich authenticates on another service (WCF Service) with username and password and gets it's data from there.

Since my WebAPI is a REST Service, it is stateless. In my opinion this means that every servicecall from my Phone to the WEBAPI leads to a new authentication process from my WebAPI on the WCF-Service. Right so far?

Now how could this be done?
Do I have to send the username/password combination every time from my phone to my WebAPI, so that the webAPI can authenticate on the other WCFService?

Or is the recommended way to kind of map the authentication-token (which would not have to contain username and password then) to a username/password combination on the WebAPI Service?

I could not find a simple solution for this. Does anybody have an idea? I would need a simple example of client-side and server-side code for understanding how this could be done.

My thoughts were:

1. Send username and password from phone to WebAPI
2. Send authentication token back to phone
3. Call another WebApi - RESTService from phone and send authentication token in header
4. ?? How do I validate this token on the WebAPI Service and how do I connect it to a user?


Could anyone write a simple piece of dummy-code?

Thank you very much!
Posted
Updated 15-Apr-14 23:34pm
v2

1 solution

I've had this same problem before. Everything is stateless apart from the concept of an authenticated session.

You could send username/password on each call but that's bobbins.

All the other alternatives I think require some state to be stored on the server. We provided a basic Login function (username/password) which if authenticated created a 'session' containing the authorization information. We used a GUID to act a key to this, so on successful authentication this is passed back.

Then subsequent calls have something like ?token=<guid>&this=that in the url.

Sessions need some sort of time-to-live (TTL) built into them so they expire after a while. If the client gets a session expired message, it must be prepared to resend the login request prior to retrying the actual request. The TTL is updated on each call.

Of course, you'll need TLS because that GUID effectively becomes you for the duration of the session.

Not saying this is a great solution, but it worked.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900