I am using basic authentication for API security by overriding the onActionExecuting Event of the filter. Once authenticated I would like to use the user object in my API controller to filter the entity db for the specific user. Any ideas on how I can get /
send the user object through to the controller? I don’t have the luxury of moving to a u ique host name per tenant / user and therefore have to filter with user authenitcation details. Here is a snippet of the filter code. Note the user object.
Any help would be appreciated
public class AuthenticationFilter : System.Web.Http.Filters.ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
DL_Login dlLogin = new DL_Login();
if (actionContext.Request.Headers.Authorization == null)
{
actionContext.Response = new System.Net.Http.HttpResponseMessage(System.Net.HttpStatusCode.Unauthorized);
}
else
{
string authenticationToken = actionContext.Request.Headers.Authorization.Parameter;
string decodedToken = Encoding.UTF8.GetString(Convert.FromBase64String(authenticationToken));
string userName = decodedToken.Substring(0, decodedToken.IndexOf(":"));
string userPassword = decodedToken.Substring(decodedToken.IndexOf(":") + 1);
var user = dlLogin.ValidateLogin(userName, userPassword, 1);
if (user.Username == null)
{
// returns unauthorized error
actionContext.Response = new System.Net.Http.HttpResponseMessage(System.Net.HttpStatusCode.Unauthorized);
}
}
}
}
Any help would be appreciated
public class AuthenticationFilter : System.Web.Http.Filters.ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
DL_Login dlLogin = new DL_Login();
if (actionContext.Request.Headers.Authorization == null)
{
actionContext.Response = new System.Net.Http.HttpResponseMessage(System.Net.HttpStatusCode.Unauthorized);
}
else
{
string authenticationToken = actionContext.Request.Headers.Authorization.Parameter;
string decodedToken = Encoding.UTF8.GetString(Convert.FromBase64String(authenticationToken));
string userName = decodedToken.Substring(0, decodedToken.IndexOf(":"));
string userPassword = decodedToken.Substring(decodedToken.IndexOf(":") + 1);
var user = dlLogin.ValidateLogin(userName, userPassword, 1);
if (user.Username == null)
{
// returns unauthorized error
actionContext.Response = new System.Net.Http.HttpResponseMessage(System.Net.HttpStatusCode.Unauthorized);
}
}
}
}