D
David B. Bitton
For some odd reason, despite the fact that I assign my own custom IPrincipal
to the HttpContext.User property in an HttpApplication.AuthenticateRequest
event handler inside of an IHttpModule, when I check the Page.User property,
it's a WindowsPrincipal and not _my_ custom Iprincipal. Why would this be?
....
using System;
using System.Web;
using System.Security.Principal;
namespace ImpersonationSample
{
/// <summary>
/// Summary description for AuthenticationModule.
/// </summary>
public class AuthenticationModule : IHttpModule
{
private IPrincipal contextUser;
public AuthenticationModule()
{
//
// TODO: Add constructor logic here
//
}
#region IHttpModule Members
public void Init(HttpApplication context)
{
context.AuthenticateRequest +=
new
EventHandler(context_AuthenticateRequest);
}
public void Dispose()
{
// TODO: Add AuthenticationModule.Dispose implementation
}
#endregion
private void context_AuthenticateRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
HttpRequest request = context.Request;
HttpResponse response = context.Response;
contextUser = context.User;
if(request.IsAuthenticated)
{
/**
* If the calling user is authenticated via NTLM by
* IIS, then we can derive Windows user info.
**/
// create our own custom User object
CustomUser user = new CustomUser(contextUser.Identity.Name);
// assign _our_ IPrincipal to the current request.
contextUser = new CustomPrincipal(contextUser.Identity, user);;
}
}
}
}
using System;
using System.Security.Principal;
namespace ImpersonationSample
{
/// <summary>
/// Summary description for CustomPrincipal.
/// </summary>
public class CustomPrincipal : IPrincipal
{
private IIdentity identity;
private CustomUser user;
public CustomPrincipal(IIdentity identity, CustomUser user)
{
this.identity = identity;
this.user = user;
}
public IIdentity Identity
{
get{ return identity; }
}
public string Username
{
get{ return user.Username; }
}
public bool IsInRole(string role)
{
return true;
}
}
}
using System;
namespace ImpersonationSample
{
/// <summary>
/// Summary description for CustomUser.
/// </summary>
public class CustomUser
{
private string name;
public CustomUser(string username)
{
this.name = username.Split('\\')[1];
}
public string Username
{
get{ return Username; }
}
}
}
....
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
UsernameLabel.Text = ((CustomPrincipal)HttpContext.Current.User).Username;
}
....
--
--
David B. Bitton
(e-mail address removed)
www.codenoevil.com
Code Made Fresh DailyT
to the HttpContext.User property in an HttpApplication.AuthenticateRequest
event handler inside of an IHttpModule, when I check the Page.User property,
it's a WindowsPrincipal and not _my_ custom Iprincipal. Why would this be?
....
using System;
using System.Web;
using System.Security.Principal;
namespace ImpersonationSample
{
/// <summary>
/// Summary description for AuthenticationModule.
/// </summary>
public class AuthenticationModule : IHttpModule
{
private IPrincipal contextUser;
public AuthenticationModule()
{
//
// TODO: Add constructor logic here
//
}
#region IHttpModule Members
public void Init(HttpApplication context)
{
context.AuthenticateRequest +=
new
EventHandler(context_AuthenticateRequest);
}
public void Dispose()
{
// TODO: Add AuthenticationModule.Dispose implementation
}
#endregion
private void context_AuthenticateRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
HttpRequest request = context.Request;
HttpResponse response = context.Response;
contextUser = context.User;
if(request.IsAuthenticated)
{
/**
* If the calling user is authenticated via NTLM by
* IIS, then we can derive Windows user info.
**/
// create our own custom User object
CustomUser user = new CustomUser(contextUser.Identity.Name);
// assign _our_ IPrincipal to the current request.
contextUser = new CustomPrincipal(contextUser.Identity, user);;
}
}
}
}
using System;
using System.Security.Principal;
namespace ImpersonationSample
{
/// <summary>
/// Summary description for CustomPrincipal.
/// </summary>
public class CustomPrincipal : IPrincipal
{
private IIdentity identity;
private CustomUser user;
public CustomPrincipal(IIdentity identity, CustomUser user)
{
this.identity = identity;
this.user = user;
}
public IIdentity Identity
{
get{ return identity; }
}
public string Username
{
get{ return user.Username; }
}
public bool IsInRole(string role)
{
return true;
}
}
}
using System;
namespace ImpersonationSample
{
/// <summary>
/// Summary description for CustomUser.
/// </summary>
public class CustomUser
{
private string name;
public CustomUser(string username)
{
this.name = username.Split('\\')[1];
}
public string Username
{
get{ return Username; }
}
}
}
....
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
UsernameLabel.Text = ((CustomPrincipal)HttpContext.Current.User).Username;
}
....
--
--
David B. Bitton
(e-mail address removed)
www.codenoevil.com
Code Made Fresh DailyT