My requirements
I have a Web API which gives me required data from the database. I have a .Net website which consumes this Web API to get all data. Now the requirement is to protect the APIs from being exposed to anyone on the web.
What I have done so far:
On the website:
I generate a token when the user logs in. This token holds the details like username, a random key and the expiration time of the token, all in an encrypted string.
On the API:
Upon every request, before sending the response, I decrypt the token and validate if the username, key and expiration time are valid. If all of them are valid, the response will be sent. Otherwise, an error message is sent in response. This is the function that I made changes in:
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
AuthenticationHeaderValue authorization = request.Headers.Authorization;
if (authorization != null)
{
if (Encryption.DecryptData(token).Equals(ConfigurationManager.AppSettings["TokenKey"].ToString()))
{
//Send response
}
}
}
Everything is good till here.
Now the problem is...
Let us say that I put an expiration time of 15 minutes for the token. The generated token hence can be used for 15 minutes after login to get responses from the API. This token can be used irrespective of the user's login status i.e, the token is valid for 15 minutes even if the user signs out immediately after logging in.
I have been brainstorming and searching for solutions for the past few days and I came across the thought of handling the data in sessions but found later that it is a bad idea with respect to scalability.
There are also constraints that I cannot use OWIN. Additionally, the project is not MVC, it is a Web API project.
Can someone please come to my rescue and point me towards a solution? I'll be grateful as this has already consumed a lot of time.