namespace WebApplication.App_Start
{
    public class WFFormPrincipal : IPrincipal
    {
        public IIdentity Identity { get; private set; }
        //用户数据
        public WFFormsAuthentication UserData { get; private set; }        public WFFormPrincipal ( FormsAuthenticationTicket ticket, WFFormsAuthentication userData )
        {
            if (ticket == null)
                throw new ArgumentNullException("ticket");
            if (userData == null)
                throw new ArgumentNullException("userData");            Identity = new FormsIdentity(ticket);
            UserData = userData;
        }        //角色验证
        public bool IsInRole ( string role )
        {
            return false;
        }
    }    public class WFFormsAuthentication
    {
        public string SessionId { get; set; }        public string UserId { get; set; }        public string Name { get; set; }        public string TrueName { get; set; }        public string LoginTime { get; set; }
        private const int CookieSaveDays = 20;        public static string SetAuthCookie ( string username, WFFormsAuthentication userData, bool remember )
        {
            if (userData == null)
                throw new ArgumentNullException("userData");            var data = Newtonsoft.Json.JsonConvert.SerializeObject(userData);            var expires = remember ? DateTime.Now.AddDays(30) : DateTime.Now.AddDays(1);            var ticket = new FormsAuthenticationTicket(1, username, DateTime.Now, expires, true, data);            var cookieValue = FormsAuthentication.Encrypt(ticket);            var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, cookieValue)
            {
                HttpOnly = false,
                Secure = FormsAuthentication.RequireSSL,
                Domain = FormsAuthentication.CookieDomain,
                Path = FormsAuthentication.FormsCookiePath,
            };            cookie.Expires = expires;
            HttpContext context = HttpContext.Current;
            if (context == null)
                throw new InvalidOperationException();            context.Response.Cookies.Remove(cookie.Name);            context.Response.Cookies.Add(cookie);
            return cookieValue;
            //context.Response.Cookies.Add(new HttpCookie("test", DateTime.Now.ToString("yyyyMMdd HHmmss")) {  Expires=DateTime.Now.AddDays(1)});
        }        public static void SignOut ( )
        {
            var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, "")
            {
                HttpOnly = true,
                Secure = FormsAuthentication.RequireSSL,
                Domain = FormsAuthentication.CookieDomain,
                Path = FormsAuthentication.FormsCookiePath,
                Expires = DateTime.Now.AddDays(-1)
            };
            HttpContext context = HttpContext.Current;
            if (context == null)
                throw new InvalidOperationException();
            context.Response.Cookies.Add(cookie);
        }    
        public static WFFormPrincipal TryParsePrincipal ( HttpContext context )
        {
            if (context == null || context.Request == null)
                throw new ArgumentNullException("context");            var cookie = context.Request.Cookies[FormsAuthentication.FormsCookieName];            if (cookie != null && !string.IsNullOrEmpty(cookie.Value))
            {                try
                {
                    var ticket = FormsAuthentication.Decrypt(cookie.Value);
                    if (ticket != null && !string.IsNullOrEmpty(ticket.UserData))
                    {
                        var userData = Newtonsoft.Json.JsonConvert.DeserializeObject<WFFormsAuthentication>(ticket.UserData);
                        if (userData != null)
                        {
                            return new WFFormPrincipal(ticket, userData);
                            //return IsValid(userData.SessionId) ? new WFFormPrincipal(ticket, userData) : null;
                        }
                    }
                }
                catch
                {
                }
            }            return null;
        }
    }
}
//我这里是通过写cookie方式实现用户登录  但是服务器做了负载均衡后就一直登录不上去