Skip to content

Instantly share code, notes, and snippets.

@ChrisMcKee
Created March 20, 2013 20:55
Show Gist options
  • Save ChrisMcKee/5208336 to your computer and use it in GitHub Desktop.
Save ChrisMcKee/5208336 to your computer and use it in GitHub Desktop.
FormsAuthentication class + AuthCookie
namespace xxx.Web.Auth
{
using System;
using System.Globalization;
using System.Web;
using System.Web.Security;
using Domain.Entities;
public class AuthCookie
{
private static readonly string Delimiter = Environment.NewLine;
public AuthCookie(string identifier)
{
Identifier = identifier;
int parsedId;
if (Int32.TryParse(identifier, out parsedId)) Id = parsedId;
}
public int? Id { get; set; }
public string Identifier { get; set; }
public string DisplayName { get; set; }
public string Email { get; set; }
public RoleType RoleType { get; set; }
public static AuthCookie Get()
{
if (!HttpContext.Current.User.Identity.IsAuthenticated)
return null;
var authCookie = new AuthCookie(HttpContext.Current.User.Identity.Name);
if (!(HttpContext.Current.User.Identity is FormsIdentity))
return authCookie;
var identity = (FormsIdentity) HttpContext.Current.User.Identity;
if (string.IsNullOrWhiteSpace(identity.Ticket.UserData))
return authCookie;
string[] data = identity.Ticket.UserData.Split(new[] {Delimiter}, StringSplitOptions.None);
authCookie.DisplayName = data[0];
if (data.Length > 1)
authCookie.Email = data[1];
if (data.Length > 2)
{
RoleType roleType;
Enum.TryParse<RoleType>(data[2], true, out roleType);
authCookie.RoleType = roleType;
}
return authCookie;
}
public void Set()
{
DateTime now = DateTime.Now;
string data = DisplayName + Delimiter + Email + Delimiter + RoleType;
var ticket = new FormsAuthenticationTicket(1, Identifier, now, now + System.Web.Security.FormsAuthentication.Timeout, true, data);
string encryptedTicket = System.Web.Security.FormsAuthentication.Encrypt(ticket);
var cookie = new HttpCookie(System.Web.Security.FormsAuthentication.FormsCookieName, encryptedTicket)
{
Expires = now + System.Web.Security.FormsAuthentication.Timeout
};
HttpContext.Current.Response.Cookies.Add(cookie);
}
}
}
namespace xxx.Web.Auth
{
using System;
using System.Security.Principal;
using xxx.Domain.Entities;
public class FormsAuthentication : IFormsAuthentication
{
public void SetAuthCookie(string userId, RoleType roleType, string displayName, string email)
{
var identityName = string.Format("{0}|{1}", userId, roleType);
var authCookie = new AuthCookie(identityName)
{
DisplayName = displayName,
Email = email,
RoleType = roleType
};
authCookie.Set();
}
public AuthCookie GetAuthCookie()
{
try
{
return AuthCookie.Get();
}
catch (Exception)
{
}
return null;
}
public void SignOut()
{
System.Web.Security.FormsAuthentication.SignOut();
}
}
public static class IdentityExtensions
{
internal class UserIdentity
{
public UserIdentity(int userId, RoleType roleType)
{
UserId = userId;
RoleType = roleType;
}
public int UserId { get; set; }
public RoleType RoleType { get; set; }
}
private static UserIdentity GetIdentity(this IIdentity identity)
{
RoleType currentIdentityRole;
int currentIdentityUserId;
if (identity.Name == null) return null;
var split = identity.Name.Split('|');
if (split.Length < 1) return null;
int.TryParse(split[0], out currentIdentityUserId);
Enum.TryParse(split[1], out currentIdentityRole);
return new UserIdentity(currentIdentityUserId, currentIdentityRole);
}
public static bool IsInRole(this IIdentity identity, RoleType role)
{
var currenRole = identity.CurrentRole();
return currenRole.HasValue && currenRole.Value.Equals(role);
}
public static RoleType? CurrentRole(this IIdentity identity)
{
var role = identity.GetIdentity();
if (role == null)
{
System.Web.Security.FormsAuthentication.SignOut();
return null;
}
return role.RoleType;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment