Created
August 23, 2013 03:17
-
-
Save ssajous/6315210 to your computer and use it in GitHub Desktop.
Components to generate secure tokens to be created by a secure web service to use for authentication of subsequent calls. It essentially operates on the same premise of a remember me cookie in forms authentication. The token expiration should use configured values instead of hard coding... This example uses JSON .NET to serialize objects into js…
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
| public interface IStringSerializer | |
| { | |
| T Deserialize<T>(string item); | |
| string Serialize(object item); | |
| } |
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
| public interface ITokenProvider | |
| { | |
| string GenerateToken<T>(TokenInfo<T> data); | |
| TokenInfo<T> ParseToken<T>(string token); | |
| } |
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
| public class JsonSerializer : IStringSerializer | |
| { | |
| public T Deserialize<T>(string item) | |
| { | |
| return JsonConvert.DeserializeObject<T>(item); | |
| } | |
| public string Serialize(object item) | |
| { | |
| return JsonConvert.SerializeObject(item); | |
| } | |
| } |
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
| public class SecurityTokenProvider : ITokenProvider | |
| { | |
| private readonly IStringSerializer _serializer; | |
| public SecurityTokenProvider(IStringSerializer serializer) | |
| { | |
| if (serializer == null) throw new ArgumentNullException("serializer"); | |
| _serializer = serializer; | |
| } | |
| public string GenerateToken<T>(TokenInfo<T> data) | |
| { | |
| FormsAuthenticationTicket ticket = new FormsAuthenticationTicket( | |
| 1, | |
| data.UserName, | |
| DateTime.Now, | |
| DateTime.Now.AddMinutes(20), | |
| false, | |
| _serializer.Serialize(data.UserData) | |
| ); | |
| string token = FormsAuthentication.Encrypt(ticket); | |
| return token; | |
| } | |
| public TokenInfo<T> ParseToken<T>(string token) | |
| { | |
| FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(token); | |
| if (ticket != null) | |
| { | |
| var userData = _serializer.Deserialize<T>(ticket.UserData); | |
| var info = new TokenInfo<T> | |
| { | |
| UserName = ticket.Name, | |
| UserData = userData | |
| }; | |
| return info; | |
| } | |
| // Null ticket = invalid token | |
| throw new InvalidOperationException(); | |
| } | |
| } |
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
| public class TokenInfo<T> | |
| { | |
| public string UserName { get; set; } | |
| public T UserData { get; set; } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment