P
Peter Larsen [CPH]
Hi,
I have a question about Thread.CurrentPrincipal and whether it is necessary
to set CurrentPricipal in code or not.
See the following sample:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
string s = GetTime();
}
[PrincipalPermission(SecurityAction.Demand,Role=@"DOMAIN\USERS")]
private string GetTime()
{
return DateTime.Now.ToString();
}
}
Running this code, will produce an exception, since CurrentPricipal return
an "empty" IPrincipal.
Setting CurrentPricipal myself (like the following samplecode) will do the
trick and GetTime() will be executed if i have the USERS role.
public Form1()
{
InitializeComponent();
Thread.CurrentPrincipal = new
WindowsPrincipal(WindowsIdentity.GetCurrent());
}
My question is - Is it really necessary to this, like the above sample, or
is there a better way (e.g. best practice) to handle a situation like this.
Thank you in advance.
BR
Peter
I have a question about Thread.CurrentPrincipal and whether it is necessary
to set CurrentPricipal in code or not.
See the following sample:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
string s = GetTime();
}
[PrincipalPermission(SecurityAction.Demand,Role=@"DOMAIN\USERS")]
private string GetTime()
{
return DateTime.Now.ToString();
}
}
Running this code, will produce an exception, since CurrentPricipal return
an "empty" IPrincipal.
Setting CurrentPricipal myself (like the following samplecode) will do the
trick and GetTime() will be executed if i have the USERS role.
public Form1()
{
InitializeComponent();
Thread.CurrentPrincipal = new
WindowsPrincipal(WindowsIdentity.GetCurrent());
}
My question is - Is it really necessary to this, like the above sample, or
is there a better way (e.g. best practice) to handle a situation like this.
Thank you in advance.
BR
Peter