I have an Asp.Net Core REST service. My boss didn’t want to use JWT to keep things simple, so we’re just putting the username / password in the request:
{
“userName”: “Bob”,
“password”: “password”,
...
}
All my controllers derive from my own ControllerBase class which has a snippet:
public override void OnActionExecuting(ActionExecutingContext context)
{
if (ModelState.IsValid)
{
RequestBase request = (RequestBase)context.ActionArguments.Where(x => x.Value is RequestBase).First().Value;
if (!OnValidateCredentials(request.Username, request.Password))
context.Result = Unauthorized();
else
base.OnActionExecuting(context);
}
...
}
The OnValidateCredentials method just hits up a database and grabs a User object and then validates the password hash. That's all cool.
So question... I'm not really sure how threading works in Core REST, but is it safe to store the User object in ControllerBase as a property so the derived controller can access it? Or is it possible to get multiple calls into the same instance of the controller simultaneously? If so... how can I store the User object so the derived class / method can access it without it getting stomped on by other threads?