Created
April 25, 2014 21:33
stateless auth for nancy
This file contains 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
public static class Authentication { | |
public const string Secret = "123@123"; | |
public static void Setup(IPipelines pipelines, Db db) { | |
var c = new StatelessAuthenticationConfiguration(ctx => { | |
var username = ctx.Request.Headers[Constants.HeaderUsername].FirstOrDefault(); | |
var hash = ctx.Request.Headers[Constants.HeaderHash].FirstOrDefault(); | |
if (String.IsNullOrEmpty(username)) { | |
return null; | |
} | |
if (String.IsNullOrEmpty(hash)) { | |
return null; | |
} | |
if (hash != HashUsername(username)) { | |
return null; | |
} | |
var user = db.Users.FirstOrDefault(u => u.Username == username); | |
if (user == null) { | |
return null; | |
} | |
return new UserIdentity { | |
UserName = username, | |
User = user.AsDTO() | |
}; | |
}); | |
StatelessAuthentication.Enable(pipelines, c); | |
} | |
public static string HashUsername(string username) { | |
return String.Concat(username, Secret).Md5Hash(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment