I have an application in MVC 4, ASP.NET 4.5 and Windows Authentication. I'm trying to extend the Identity and Principal objects (WindowsIdentity and WindowsPrincipal respectively) in order to provide additional information about the user logged on, but when I try to create the extended instance of these objects and replace the Current.User it throws an error:
System.UnauthorizedAccessException: "Attempted to perform an unauthorized operation."
Below the code I'm using in the global.asax:
public void WindowsAuthentication_OnAuthenticate(object sender, WindowsAuthenticationEventArgs args)
{
if (!args.Identity.IsAnonymous)
{
var userData = WindowsUserDataHelper.GetWindowsUserData(args.Identity);
var user = new MyCustomPrincipal(new MyCustomIdentity(args.Identity.Name, userData));
HttpContext.Current.User = user; //-- exception thrown here
Thread.CurrentPrincipal = user;
}
}
Here is the web.config settings:
<authentication mode="Windows" >
</authentication>
<authorization>
<deny users="?" />
</authorization>
And in my local IIS I have set the authentication this way:
- Anonymous: Disabled
- Basic: Disabled
- Windows Authentication: Enabled
- Forms Authentication: Disabled
- ASP.NET Impersonation: Disabled