[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class SSOService : ISSOService, ISSOPartnerService
{ #region ISSOService Members
public SSOToken RequestToken()
{ SSOToken token = new SSOToken
{ Token = string.Empty,
Status = "DENIED"
};
if (HttpContext.Current.Request.IsAuthenticated)
{ FormsIdentity identity = (FormsIdentity)HttpContext.Current.User.Identity;
token.Token = FormsAuthentication.Encrypt(identity.Ticket);
token.Status = "SUCCESS";
}
return token;
}
public bool Logout()
{ HttpContext.Current.Session.Clear();
FormsAuthentication.SignOut();
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName);
cookie.Expires = DateTime.Now.AddDays(-10000.0);
HttpContext.Current.Response.Cookies.Add(cookie);
return true;
}
public SSOToken Login(string username, string password)
{ SSOToken token = new SSOToken
{ Token = string.Empty,
Status = "DENIED"
};
// authenticate user
if (string.CompareOrdinal("foo", username) == 0 && string.CompareOrdinal("bar", password) == 0) { Guid temp = Guid.NewGuid();
DateTime issueDate = DateTime.Now;
DateTime expireDate = issueDate.AddMonths(1);
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, username, issueDate, expireDate, true, temp.ToString());
string protectedTicket = FormsAuthentication.Encrypt(ticket);
HttpCookie authorizationCookie = new HttpCookie(FormsAuthentication.FormsCookieName, protectedTicket);
authorizationCookie.Expires = expireDate;
authorizationCookie.HttpOnly = true;
HttpContext.Current.Response.Cookies.Add(authorizationCookie);
token.Status = "SUCCESS";
token.Token = protectedTicket;
}
return token;
}
public SSOUser ValidateToken(string token)
{ try
{ FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(token);
return new SSOUser { Username = ticket.Name,
SessionToken = new Guid(ticket.UserData)
};
}
catch
{ return new SSOUser { Username = string.Empty,
SessionToken = Guid.Empty
};
}
}
#endregion
}