I need to authenticate the incoming request before it start processing. When my client application requests to the server, I need to authenticate the request using basic authentication and need to send response to the client.
I've tried the following but failed,
public class OptionalAuthentication : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
HttpContextWrapper httpContext = new HttpContextWrapper(System.Web.HttpContext.Current);
HttpRequestBase httpRequest = httpContext.Request;
if ((httpContext.User == null ? true : !httpContext.User.Identity.IsAuthenticated))
{
var request = HttpContext.Current.Request;
//request.Headers.FirstOrDefault(h => h.Key.Equals("Authorization"));
var authHeader = request.Headers["Authorization"];
if (authHeader != null)
{
var authHeaderVal = AuthenticationHeaderValue.Parse(authHeader);
// RFC 2617 sec 1.2, "scheme" name is case-insensitive
if (authHeaderVal.Scheme.Equals("basic",
StringComparison.OrdinalIgnoreCase) &&
authHeaderVal.Parameter != null)
{
if (AuthenticateUser(authHeaderVal.Parameter))
{
}
}
}
}
else
{
//log.Trace("user is already authenticated: '{0}'", httpContext.User.Identity.Name);
}
}
}
[OptionalAuthentication]
public ActionResult Index(string projectSlug, string repositoryName)
{
ActionResult emptyResult;
if (Request.IsAuthenticated)
{
var cred = System.Text.ASCIIEncoding.ASCII.GetString(Convert.FromBase64String(Request.Headers["Authorization"].Substring(6))).Split(':');
}
}
can somebody tell me a way to achieve this.