2

I am new to web application. I have created a custom principle and trying to set it in CurrentDomain. This code works perfectly in WPF application. But here it is throwing Policy Exception "Default principal object cannot be set twice."

 var principal = new CustomPrinciple(currentIdentity);
 Thread.CurrentPrincipal = principal;
 AppDomain.CurrentDomain.SetThreadPrincipal(Thread.CurrentPrincipal);

My CustomPrincple is derived from ClaimsPrinciple

  public class CustomPrinciple : ClaimsPrincipal
  { }

I am wondering why I am not allowed to set it here. How can I set my custom principle in web application.

cdonner
  • 37,019
  • 22
  • 105
  • 153
D J
  • 6,908
  • 13
  • 43
  • 75
  • possible duplicate of [ASP.NET MVC - Set custom IIdentity or IPrincipal](http://stackoverflow.com/questions/1064271/asp-net-mvc-set-custom-iidentity-or-iprincipal) – AechoLiu Mar 10 '15 at 09:11

2 Answers2

2

The custom principal should be set the in global.asax file for the request made, not the thread. Then you get hold of the custom principal from HttpContext.User Property

This answer in this question details the best way to do it: ASP.NET MVC - Set custom IIdentity or IPrincipal

Community
  • 1
  • 1
marvc1
  • 3,641
  • 3
  • 25
  • 34
1

You are not allowed to cal SetDomainPrincipal - instead set Thread.CurrentPrincipal and HttpContext.User

http://leastprivilege.com/2012/10/08/custom-claims-principals-in-net-4-5/

leastprivilege
  • 18,196
  • 1
  • 34
  • 50