473,804 Members | 3,018 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 3713
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
3159
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
3758
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
3087
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 transaction log
4
8130
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. Catastrophic failure Description: An unhandled exception occurred during the execution of the current web...
5
5556
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 can get around this by using the Lock method of the CCriticalSection object but the accepted way...
0
2664
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. Can anyone help me figure out how to solve this? Error is as follows. PLATFORM VERSION INFO...
66
3650
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
901
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
1679
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
9706
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10578
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10332
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9152
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7620
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6853
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2991
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.