Created
May 30, 2017 09:57
-
-
Save atitsbest/61051f0b871b9b9adeb15f09b09e1f39 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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