Skip to content

Instantly share code, notes, and snippets.

@rarous
Last active September 5, 2015 06:15
Show Gist options
  • Save rarous/448368 to your computer and use it in GitHub Desktop.
Save rarous/448368 to your computer and use it in GitHub Desktop.
// Generická šablona s Implementací IPricipal
public abstract class WebUser<TUser> : IPrincipal {
public WebUser(TUser user, FormsAuthenticationTicket ticket) {
if (user == null) {
throw new ArgumentNullException("user", "user is null.");
}
User = user;
Ticket = ticket;
}
public TUser User { get; private set; }
public FormsAuthenticationTicket Ticket { get; private set; }
public IIdentity Identity {
get {
return new FormsIdentity(Ticket);
}
}
public abstract bool IsInRole(string role);
}
// Konkrétní implementace
public class WebUser : WebUser<User> {
/// <summary>
/// Initializes a new instance of the IntranetUser class.
/// </summary>
public WebUser(User user, FormsAuthenticationTicket ticket)
: base(user, ticket) {
}
public override bool IsInRole(string role) {
return (from r in User.Roles
where String.Compare(role, r.Name, ignoreCase: true) == 0
select r).
Any();
}
}
// Dekorátor pro Forms authentikaci
public class WebUserAuthenticationService : IUserAuthenticationService {
readonly IUserAuthenticationService _inner;
readonly IUsersRepository _repository;
readonly IFormsAuthentication _formsAuthentication;
/// <summary>
/// Initializes a new instance of the WebUserAuthenticationService class.
/// </summary>
/// <param name="inner"></param>
/// <param name="context"></param>
/// <param name="repository"></param>
public WebUserAuthenticationService(
IUserAuthenticationService inner,
IUsersRepository repository,
IFormsAuthentication formsAuthentication) {
_inner = inner;
_repository = repository;
_formsAuthentication = formsAuthentication;
}
public void ChangePassword(string userName, string newPassword) {
_inner.ChangePassword(userName, newPassword);
}
public bool Authenticate(string userName, string password) {
var isAuthenticated = _inner.Authenticate(userName, password);
if (isAuthenticated) {
SetUserToContext(userName);
}
return isAuthenticated;
}
public void SetUserToContext(string userName) {
if (userName.IsNullOrEmpty()) {
return;
}
var user = _repository.FindByUserName(userName);
var ticket = _formsAuthentication.GetTicket();
if (ticket == null) {
return;
}
HttpContext.Current.User = new WebUser(user, ticket);
}
}
// Forms authentication wrapper
public class FormsAuthenticationWrapper : IFormsAuthentication {
public void SetAuthCookie(string userName, bool createPersistentCookie) {
FormsAuthentication.SetAuthCookie(userName, createPersistentCookie);
}
public void SignOut() {
FormsAuthentication.SignOut();
}
public string GetLoggedUserName() {
var authTicket = GetTicket();
if (authTicket == null) {
return String.Empty;
}
return authTicket.Name;
}
public FormsAuthenticationTicket GetTicket() {
var cookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
if (cookie == null) {
return null;
}
return FormsAuthentication.Decrypt(cookie.Value);
}
}
// Použití v Global asax
protected void Application_AuthenticateRequest() {
if (Context.Request.IsStaticContentRequest()) {
// na obrazky styly a scripty seru, nepotřebuju ověřovat
return;
}
SetUserToContext(Container);
}
static void SetUserToContext(IWindsorContainer container) {
var formsAuthentication = container.Resolve<IFormsAuthentication>();
var authenticationService = container.Resolve<IUserAuthenticationService>();
var userName = formsAuthentication.GetLoggedUserName(); // načte usera z Forms
authenticationService.SetUserToContext(userName);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment