Skip to content

Instantly share code, notes, and snippets.

@atitsbest
Created May 30, 2017 09:57
Show Gist options
  • Save atitsbest/61051f0b871b9b9adeb15f09b09e1f39 to your computer and use it in GitHub Desktop.
Save atitsbest/61051f0b871b9b9adeb15f09b09e1f39 to your computer and use it in GitHub Desktop.
using System;
using System.Net;
using System.Net.Http;
using System.Security.Principal;
using System.Text;
using System.Threading;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
namespace PMSys_Draft.Filters
{
public class BasicAuthenticationAttribute : AuthorizationFilterAttribute
{
public override void OnAuthorization(HttpActionContext actionContext)
{
if (actionContext.Request.Headers.Authorization == null)
{
actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized);
}
else
{
var token = actionContext.Request.Headers.Authorization.Parameter;
var decodedToken = Encoding.UTF8.GetString(
Convert.FromBase64String(token));
var userpwd = decodedToken.Split(':');
var username = userpwd[0];
var pwd = userpwd[1];
if (username == "tester" && pwd == "keines")
{
Thread.CurrentPrincipal = new GenericPrincipal(
new GenericIdentity(username), null);
}
else
{
actionContext.Response = actionContext.Request
.CreateResponse(HttpStatusCode.Unauthorized);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment