473,574 Members | 2,600 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

MsTest and PrincipalPermis sionAttribute Failure

I am reposting this for a colleague because he has had some problems setting
up his subscriber Nospam alias:

I am having problems unit testing a method which is decorated with the
[PrincipalPermis sion] attribute.
I always get a security exception. I have set the Thread.CurrentP rincipal
to my custom IPrincipal.IsIn Role method never gets called

I have even tried
AppDomain.Curre ntDomain.SetThr eadPrincipal(my IPrincipal)...w ith no luck.

Here is a basic example

[PrincipalPermis sion(SecurityAc tion.Demand, Role="MyRole")]
public void DoWork()
{
}
[TestMethod]
public void TestDoWork()
{
GenericPrincipa l principal = new GenericPrincipa l(
Thread.CurrentP rincipal.Identi ty, new string[] { "MyRole" });
Thread.CurrentP rincipal = principal;
DoWork();
Assert(....)
}
Environment: (all current with MicrosoftUpdate SPs and patches)
* VS2005 Team Suite
* .NET 2.0 and 3.0 redist and SDK
* Team Foundation Server

Feb 15 '07 #1
4 3700
Hello BJ,

From your description, you're meeting some problem to get the
"PrincipalPermi ssion" demand to work in your vs 2005/.net 2.0 program,
correct?

I've performed some local tests through winform application, it seems that
the "PrincipalPermi ssionAttribute" can work correctly for custom
principal/identity or windows principal/identity associated with the
current thread. Also, according to the MSDN document, it indicate that we
should set AppDomain's PrincipalPolicy to "WindowsPrincip al" at
application's initializing time

#PrincipalPermi ssionAttribute Class
http://msdn2.microsoft.com/en-us/lib...ssions.princip
alpermissionatt ribute.aspx

My test code is as below, you can also have a test to see whether it works
for you:

=============== ========
private void btnSetPrincipal _Click(object sender, EventArgs e)
{
GenericIdentity identity = new
GenericIdentity ("testuser", "customauth ");

GenericPrincipa l gp = new GenericPrincipa l(identity, new
string[] { "role1", "role2" });

Thread.CurrentP rincipal = gp;

}

[PrincipalPermis sion( SecurityAction. Demand, Role="role1")]
private void btnDemand_Click (object sender, EventArgs e)
{
MessageBox.Show ("success demand!");
}

private void Form1_Load(obje ct sender, EventArgs e)
{

AppDomain.Curre ntDomain.SetPri ncipalPolicy(Pr incipalPolicy.W indowsPrincipal )
;
}
=============== ==========
In addition, you can try programmaticall y construct a PrincipalPermis sion
class instance and call its "Demand" method to see whether it works. Also,
make sure your principal-demand method hasn't been called on a different
thread context(you can printout the thread's principal/identity context to
verify this).

If you have any further finding, please feel free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

=============== =============== =============== =====

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.

=============== =============== =============== =====

This posting is provided "AS IS" with no warranties, and confers no rights.

Feb 16 '07 #2
Steven,

This is BJ's colleague who he is helping. There is no doubt that
[PrincipalPermis sion] works in the context you mention, but it is not working
in the context of a MSTest Unit Test. Please read thoroughly the description
of the problem. Refactor your code sample so that you can call
"btnDemand_Clic k" from a unit test and you will see the issue.

-Casey

"Steven Cheng[MSFT]" wrote:
Hello BJ,

From your description, you're meeting some problem to get the
"PrincipalPermi ssion" demand to work in your vs 2005/.net 2.0 program,
correct?

I've performed some local tests through winform application, it seems that
the "PrincipalPermi ssionAttribute" can work correctly for custom
principal/identity or windows principal/identity associated with the
current thread. Also, according to the MSDN document, it indicate that we
should set AppDomain's PrincipalPolicy to "WindowsPrincip al" at
application's initializing time

#PrincipalPermi ssionAttribute Class
http://msdn2.microsoft.com/en-us/lib...ssions.princip
alpermissionatt ribute.aspx

My test code is as below, you can also have a test to see whether it works
for you:

=============== ========
private void btnSetPrincipal _Click(object sender, EventArgs e)
{
GenericIdentity identity = new
GenericIdentity ("testuser", "customauth ");

GenericPrincipa l gp = new GenericPrincipa l(identity, new
string[] { "role1", "role2" });

Thread.CurrentP rincipal = gp;

}

[PrincipalPermis sion( SecurityAction. Demand, Role="role1")]
private void btnDemand_Click (object sender, EventArgs e)
{
MessageBox.Show ("success demand!");
}

private void Form1_Load(obje ct sender, EventArgs e)
{

AppDomain.Curre ntDomain.SetPri ncipalPolicy(Pr incipalPolicy.W indowsPrincipal )
;
}
=============== ==========
In addition, you can try programmaticall y construct a PrincipalPermis sion
class instance and call its "Demand" method to see whether it works. Also,
make sure your principal-demand method hasn't been called on a different
thread context(you can printout the thread's principal/identity context to
verify this).

If you have any further finding, please feel free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

=============== =============== =============== =====

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.

=============== =============== =============== =====

This posting is provided "AS IS" with no warranties, and confers no rights.

Feb 16 '07 #3
Thanks for the reply, Steven.

It appears that setting the App domain's PrincipalPolicy is the key issue.
Based on your response, we found that we have to set the PrincipalPolicy as a
part of the initialization of each test for it to work in the testing
environment.

Thanks again for the prompt reply.

--BJ Safdie

"Steven Cheng[MSFT]" wrote:
Hello BJ,

From your description, you're meeting some problem to get the
"PrincipalPermi ssion" demand to work in your vs 2005/.net 2.0 program,
correct?

I've performed some local tests through winform application, it seems that
the "PrincipalPermi ssionAttribute" can work correctly for custom
principal/identity or windows principal/identity associated with the
current thread. Also, according to the MSDN document, it indicate that we
should set AppDomain's PrincipalPolicy to "WindowsPrincip al" at
application's initializing time

#PrincipalPermi ssionAttribute Class
http://msdn2.microsoft.com/en-us/lib...ssions.princip
alpermissionatt ribute.aspx

My test code is as below, you can also have a test to see whether it works
for you:

=============== ========
private void btnSetPrincipal _Click(object sender, EventArgs e)
{
GenericIdentity identity = new
GenericIdentity ("testuser", "customauth ");

GenericPrincipa l gp = new GenericPrincipa l(identity, new
string[] { "role1", "role2" });

Thread.CurrentP rincipal = gp;

}

[PrincipalPermis sion( SecurityAction. Demand, Role="role1")]
private void btnDemand_Click (object sender, EventArgs e)
{
MessageBox.Show ("success demand!");
}

private void Form1_Load(obje ct sender, EventArgs e)
{

AppDomain.Curre ntDomain.SetPri ncipalPolicy(Pr incipalPolicy.W indowsPrincipal )
;
}
=============== ==========
In addition, you can try programmaticall y construct a PrincipalPermis sion
class instance and call its "Demand" method to see whether it works. Also,
make sure your principal-demand method hasn't been called on a different
thread context(you can printout the thread's principal/identity context to
verify this).

If you have any further finding, please feel free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead

=============== =============== =============== =====

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.

=============== =============== =============== =====

This posting is provided "AS IS" with no warranties, and confers no rights.

Feb 16 '07 #4
Thanks for your reply BJ,

I just came back and see your followup. I also found your colleague Casey's
reply indicate that the problem may also be specfiic to the [TestMethod]
(test project) scenario, have you also got it working for that case also?
As Casey mentioned, I did only peform the test code in a normal console and
winform application and haven't do the exact test in the test project. So
if you still have the problem in that case, please feel free to let me
know, I'll help you continue work on it.

Anyway, glad that my suggestion has helped you.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.

Feb 19 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
3138
by: x2164 | last post by:
hi all, Linux 2.4.28 Glibc 2.2.5 gcc 2.95.3 I'm new to Python. I've compiled Python 2.4 from tar file.
3
3744
by: Damaji Jambhale | last post by:
COMException: Catostrphic failure When I added a "dll" reference in the web project. I was able to instantiate the class OK. But when I tried to set the properties, it failed with "COMException: Catostrphic failure". Any idea? Thanks.
2
3081
by: JustaCowboy | last post by:
Greetings, I am seeking information related to this subject. BOL suggests backing up the active transaction log immediately after a failure, so that the backup can be used in a recovery scenario if necessary. This is the relevant text from BOL "Transaction Log Backup": ----// The transaction log backup created at 8:00 P.M. contains...
4
8109
by: J. Marshall Latham | last post by:
I have written an ASP.NET web app in C# that is trying to connect to a database using OleDb. I put code in a dll that uses another dll to create a connection object (and open it if requested) to send back to the web app to connect to the database. I am getting the following error when I change anything in my web app and recompile....
5
5549
by: Ron Louzon | last post by:
I have some C++ code that uses the CSingleLock( CCriticalSection *) constructor. In visual C++ 6.0, this code compiles and runs fine in both Debug and release modes. However, in Visual Studio .Net, when I run this code I get an Assertion failure. The error appears to be exactly the same as that seen with CSingleLock in VC++ version 4.0. I...
0
2638
by: Marty Cruise | last post by:
I successfully deploy my application to 20 domain users. Only one new user is giving me a problem, although he can access all domain resources. When he clicks the installation link on the publish page, Framework 2.0 installs successfully, but then the application installation fails during the "Verifying Application Requirements" process. ...
66
3579
by: Johan Tibell | last post by:
I've written a piece of code that uses sockets a lot (I know that sockets aren't portable C, this is not a question about sockets per se). Much of my code ended up looking like this: if (function(socket, args) == -1) { perror("function"); exit(EXIT_FAILURE); } I feel that the ifs destroy the readability of my code. Would it be
0
896
by: xamman | last post by:
hello! anyone ever manage to do this? after trying nearly everything i can think of my code (below) still gives 'request for principal permissions failed' . many thanks
3
1671
by: xamman | last post by:
hello! anyone ever manage to do this? after trying nearly everything i can think of my code (below) still gives 'request for principal permissions failed' . many thanks x .. .. ..
0
7814
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8249
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7826
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6464
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5631
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5307
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3756
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2252
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1071
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.