<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://forums.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Search results matching tag 'IPrincipal'</title><link>http://forums.asp.net/search/SearchResults.aspx?q=&amp;tag=IPrincipal&amp;orTags=0&amp;o=DateDescending</link><description>Search results matching tag 'IPrincipal'</description><dc:language>en-US</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>Custom RoleProvider and Custom Principal</title><link>http://forums.asp.net/thread/2379172.aspx</link><pubDate>Sun, 25 May 2008 04:00:00 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:2379172</guid><dc:creator>french_duke</dc:creator><description>&lt;p&gt;Hi folks&lt;/p&gt;
&lt;p&gt;I am implementing asp.net 2.0 roles and authentication for the first time so bear with me if my ideas are a bit mixed-up!&lt;/p&gt;
&lt;p&gt;I have written a CustomRoleProvider class which overrides all the necessary members required to use my custom datastore for user roles - and this appears to be working great. Next, I &lt;a class="" href="http://forums.asp.net/t/1262847.aspx" target="_blank"&gt;posted&lt;/a&gt; asking&amp;nbsp; how to store extra information for my user as&amp;nbsp;I require access to&amp;nbsp;a userId (representing the primary key from my users table), companyId and company name (similar database columns).&lt;/p&gt;
&lt;p&gt;I was advised to create a Custom Principal class (implementing the IPrincipal interface), and to add properties to this class to store the data I need, then on the Application_AuthenticateRequest event I initialise my CustomPrincipal object, sending in the data from the database. Here is the code:&lt;/p&gt;
&lt;p&gt;CustomPrincipal.cs&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;pre class="coloredcode"&gt;&lt;span class="kwd"&gt;public class&lt;/span&gt; CustomPrincipal : IPrincipal
{
    &lt;span class="kwd"&gt;#region&lt;/span&gt;&lt;span class="kwdt"&gt; &lt;span class="st"&gt;&amp;quot;Private member variables&amp;quot;&lt;/span&gt;&lt;/span&gt;

    &lt;span class="kwd"&gt;private&lt;/span&gt; IIdentity _identity;
    &lt;span class="kwd"&gt;private int&lt;/span&gt; _userId;
    &lt;span class="kwd"&gt;private int&lt;/span&gt; _companyId;
    &lt;span class="kwd"&gt;private string&lt;/span&gt; _companyName;

    &lt;span class="kwd"&gt;#endregion

    #region&lt;/span&gt;&lt;span class="kwdt"&gt; &lt;span class="st"&gt;&amp;quot;Public member properties&amp;quot;&lt;/span&gt;&lt;/span&gt;

    &lt;span class="kwd"&gt;public&lt;/span&gt; IIdentity Identity
    {
        &lt;span class="kwd"&gt;get&lt;/span&gt;
        {
            &lt;span class="kwd"&gt;return&lt;/span&gt; _identity;
        }
    }

    &lt;span class="kwd"&gt;public int&lt;/span&gt; UserId
    {
        &lt;span class="kwd"&gt;get&lt;/span&gt;
        {
            &lt;span class="kwd"&gt;return&lt;/span&gt; _userId;
        }
        &lt;span class="kwd"&gt;set&lt;/span&gt;
        {
            _userId = &lt;span class="kwd"&gt;value&lt;/span&gt;;
        }
    }

    &lt;span class="kwd"&gt;public int&lt;/span&gt; CompanyId
    {
        &lt;span class="kwd"&gt;get&lt;/span&gt;
        {
            &lt;span class="kwd"&gt;return&lt;/span&gt; _companyId;
        }
        &lt;span class="kwd"&gt;set&lt;/span&gt;
        {
            _companyId = &lt;span class="kwd"&gt;value&lt;/span&gt;;
        }
    }

    &lt;span class="kwd"&gt;public string&lt;/span&gt; CompanyName
    {
        &lt;span class="kwd"&gt;get&lt;/span&gt;
        {
            &lt;span class="kwd"&gt;return&lt;/span&gt; _companyName;
        }
        &lt;span class="kwd"&gt;set&lt;/span&gt;
        {
            _companyName = &lt;span class="kwd"&gt;value&lt;/span&gt;;
        }
    }

    &lt;span class="kwd"&gt;#endregion

    public&lt;/span&gt; CustomPrincipal(IIdentity identity, &lt;span class="kwd"&gt;int&lt;/span&gt; userId, &lt;span class="kwd"&gt;int&lt;/span&gt; companyId, &lt;span class="kwd"&gt;string&lt;/span&gt; companyName)
    {
        _identity = identity;
        _userId = userId;
        _companyId = companyId;
        _companyName = companyName;
    }

    &lt;span class="cmt"&gt;// IPrincipal Implementation&lt;/span&gt;
    &lt;span class="kwd"&gt;public bool&lt;/span&gt; IsInRole(&lt;span class="kwd"&gt;string&lt;/span&gt; role)
    {
        &lt;span class="cmt"&gt;//How do I implement this?&lt;/span&gt;
        &lt;span class="kwd"&gt;return true&lt;/span&gt;;
    }

}&lt;/pre&gt;&amp;nbsp;&amp;nbsp; 
&lt;p&gt;Global.asax&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;pre class="coloredcode"&gt;&lt;span class="kwd"&gt;protected void&lt;/span&gt; Application_AuthenticateRequest(&lt;span class="kwd"&gt;object&lt;/span&gt; sender, EventArgs e)
    {
        &lt;span class="cmt"&gt;// Extract the forms authentication cookie&lt;/span&gt;
        &lt;span class="kwd"&gt;string&lt;/span&gt; cookieName = FormsAuthentication.FormsCookieName;
        HttpCookie authCookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName];

        &lt;span class="kwd"&gt;if&lt;/span&gt; (authCookie == &lt;span class="kwd"&gt;null&lt;/span&gt;)
            &lt;span class="kwd"&gt;return&lt;/span&gt;;

        FormsAuthenticationTicket authTicket = &lt;span class="kwd"&gt;null&lt;/span&gt;;
        &lt;span class="kwd"&gt;try&lt;/span&gt;
        {
            authTicket = FormsAuthentication.Decrypt(authCookie.Value);
        }
        &lt;span class="kwd"&gt;catch&lt;/span&gt;
        {
            &lt;span class="kwd"&gt;return&lt;/span&gt;;
        }

        &lt;span class="kwd"&gt;if&lt;/span&gt; (authTicket == &lt;span class="kwd"&gt;null&lt;/span&gt;)
            &lt;span class="kwd"&gt;return&lt;/span&gt;;
        
        &lt;span class="cmt"&gt;//Get extra user data from database&lt;/span&gt;
        UserClass u = &lt;span class="kwd"&gt;new&lt;/span&gt; UserClass(); &lt;span class="cmt"&gt;//BLL object&lt;/span&gt;
        &lt;span class="kwd"&gt;int&lt;/span&gt; userId = 0;
        &lt;span class="kwd"&gt;int&lt;/span&gt; companyId = 0;
        &lt;span class="kwd"&gt;string&lt;/span&gt; companyName = &lt;span class="kwd"&gt;string&lt;/span&gt;.Empty;

        FormsIdentity id = &lt;span class="kwd"&gt;new&lt;/span&gt; FormsIdentity(authTicket);

        UserDataSet.UserDataTable users = u.GetUserByName(id.Name);

        &lt;span class="kwd"&gt;if&lt;/span&gt; (users.Rows.Count &amp;gt; 0)
        {
            UserDataSet.UserRow user = (UserDataSet.UserRow)users.Rows[0];
            userId = user.UserID;
            
            &lt;span class="kwd"&gt;if&lt;/span&gt; (!user.IsCompanyIDNull())
            {
                companyId = user.CompanyID;
                companyName = user.CompanyName;
            }
        }
        
        &lt;span class="cmt"&gt;// This principal will flow throughout the request.&lt;/span&gt;
        CustomPrincipal principal = &lt;span class="kwd"&gt;new&lt;/span&gt; CustomPrincipal(id, userId, companyId, companyName);
        &lt;span class="cmt"&gt;// Attach the new principal object to the current HttpContext object&lt;/span&gt;
        Context.User = principal;
    }&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;My questions are:&lt;/p&gt;
&lt;p&gt;1. How do I implement the IsInRole(string role) method of the CustomPrincipal? Have I now made my CustomRolePovider redundant?&lt;br /&gt;&lt;br /&gt;2. By getting the user data on every page load (which seems to be how often the Application_AuthenticateRequest event is fired) I am making an extra round-trip to the database. Is there a more efficient&amp;nbsp;but equally secure way of persisting this data?&lt;br /&gt;&lt;br /&gt;3. Finally, should I really be creating a custom Principal to hold the extra properties, or should it really be a custom Identity?&lt;/p&gt;
&lt;p&gt;Sorry if this is a lengthy problem - I hope someone has the patience to read it!&lt;/p&gt;
&lt;p&gt;Thanks in advance!&lt;/p&gt;</description></item><item><title>Re: Role Based Windows Authentication</title><link>http://forums.asp.net/thread/2290768.aspx</link><pubDate>Fri, 11 Apr 2008 04:00:00 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:2290768</guid><dc:creator>CharlesF</dc:creator><description>&lt;p&gt;Sounds like what you need is to use a sitemap provider for your menu and have security trimming enabled.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;This should get you started in the right direction...&lt;/p&gt;
&lt;p&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/system.web.sitemapprovider.aspx"&gt;http://msdn2.microsoft.com/en-us/library/system.web.sitemapprovider.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/system.web.xmlsitemapprovider.aspx"&gt;http://msdn2.microsoft.com/en-us/library/system.web.xmlsitemapprovider.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/ms227425.aspx"&gt;http://msdn2.microsoft.com/en-us/library/ms227425.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Here&amp;#39;s my design, if you&amp;#39;re interested (not sure this is the best way or not, I&amp;#39;m self-taught):&amp;nbsp;&lt;/p&gt;
&lt;p&gt;First, I create custom Roles and manage them in my database. Then I allow roles access to directories and files in my application via the&amp;nbsp;web.config, like this...&lt;/p&gt;&lt;pre class="coloredcode"&gt;  &amp;lt;&lt;span class="tag"&gt;location&lt;/span&gt;&lt;span class="attr"&gt; path=&lt;/span&gt;&lt;span class="attrv"&gt;&amp;quot;roles.aspx&amp;quot;&lt;/span&gt;&amp;gt;
    &amp;lt;&lt;span class="tag"&gt;system.web&lt;/span&gt;&amp;gt;
      &amp;lt;&lt;span class="tag"&gt;authorization&lt;/span&gt;&amp;gt;
        &amp;lt;&lt;span class="tag"&gt;allow&lt;/span&gt;&lt;span class="attr"&gt; roles=&lt;/span&gt;&lt;span class="attrv"&gt;&amp;quot;ManageRoles&amp;quot;&lt;/span&gt;/&amp;gt;
        &amp;lt;&lt;span class="tag"&gt;deny&lt;/span&gt;&lt;span class="attr"&gt; users=&lt;/span&gt;&lt;span class="attrv"&gt;&amp;quot;*&amp;quot;&lt;/span&gt; /&amp;gt;
      &amp;lt;/&lt;span class="tag"&gt;authorization&lt;/span&gt;&amp;gt;
    &amp;lt;/&lt;span class="tag"&gt;system.web&lt;/span&gt;&amp;gt;
  &amp;lt;/&lt;span class="tag"&gt;location&lt;/span&gt;&amp;gt;
  
  &amp;lt;&lt;span class="tag"&gt;location&lt;/span&gt;&lt;span class="attr"&gt; path=&lt;/span&gt;&lt;span class="attrv"&gt;&amp;quot;groups.aspx&amp;quot;&lt;/span&gt;&amp;gt;
    &amp;lt;&lt;span class="tag"&gt;system.web&lt;/span&gt;&amp;gt;
      &amp;lt;&lt;span class="tag"&gt;authorization&lt;/span&gt;&amp;gt;
        &amp;lt;&lt;span class="tag"&gt;allow&lt;/span&gt;&lt;span class="attr"&gt; roles=&lt;/span&gt;&lt;span class="attrv"&gt;&amp;quot;ManageGroups&amp;quot;&lt;/span&gt;/&amp;gt;
        &amp;lt;&lt;span class="tag"&gt;deny&lt;/span&gt;&lt;span class="attr"&gt; users=&lt;/span&gt;&lt;span class="attrv"&gt;&amp;quot;*&amp;quot;&lt;/span&gt; /&amp;gt;
      &amp;lt;/&lt;span class="tag"&gt;authorization&lt;/span&gt;&amp;gt;
    &amp;lt;/&lt;span class="tag"&gt;system.web&lt;/span&gt;&amp;gt;
  &amp;lt;/&lt;span class="tag"&gt;location&lt;/span&gt;&amp;gt;

  &amp;lt;&lt;span class="tag"&gt;location&lt;/span&gt;&lt;span class="attr"&gt; path=&lt;/span&gt;&lt;span class="attrv"&gt;&amp;quot;users.aspx&amp;quot;&lt;/span&gt;&amp;gt;
    &amp;lt;&lt;span class="tag"&gt;system.web&lt;/span&gt;&amp;gt;
      &amp;lt;&lt;span class="tag"&gt;authorization&lt;/span&gt;&amp;gt;
        &amp;lt;&lt;span class="tag"&gt;allow&lt;/span&gt;&lt;span class="attr"&gt; roles=&lt;/span&gt;&lt;span class="attrv"&gt;&amp;quot;ManageUsers&amp;quot;&lt;/span&gt;/&amp;gt;
        &amp;lt;&lt;span class="tag"&gt;deny&lt;/span&gt;&lt;span class="attr"&gt; users=&lt;/span&gt;&lt;span class="attrv"&gt;&amp;quot;*&amp;quot;&lt;/span&gt; /&amp;gt;
      &amp;lt;/&lt;span class="tag"&gt;authorization&lt;/span&gt;&amp;gt;
    &amp;lt;/&lt;span class="tag"&gt;system.web&lt;/span&gt;&amp;gt;
  &amp;lt;/&lt;span class="tag"&gt;location&lt;/span&gt;&amp;gt;&lt;/pre&gt;
&lt;p&gt;Note: You don&amp;#39;t have to create custom roles, you can put in the Windows Group names like this...&lt;/p&gt;
&lt;p&gt;&amp;nbsp; &amp;lt;&lt;span class="tag"&gt;location&lt;/span&gt;&lt;span class="attr"&gt; path=&lt;/span&gt;&lt;span class="attrv"&gt;&amp;quot;users.aspx&amp;quot;&lt;/span&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;span class="tag"&gt;system.web&lt;/span&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;span class="tag"&gt;authorization&lt;/span&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;span class="tag"&gt;allow&lt;/span&gt;&lt;span class="attr"&gt; roles=&lt;/span&gt;&lt;span class="attrv"&gt;&amp;quot;BUILTIN\Administrators&amp;quot;&lt;/span&gt;/&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;span class="tag"&gt;deny&lt;/span&gt;&lt;span class="attr"&gt; users=&lt;/span&gt;&lt;span class="attrv"&gt;&amp;quot;*&amp;quot;&lt;/span&gt; /&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;span class="tag"&gt;authorization&lt;/span&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;span class="tag"&gt;system.web&lt;/span&gt;&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;lt;/&lt;span class="tag"&gt;location&lt;/span&gt;&amp;gt;&amp;nbsp; &lt;/p&gt;
&lt;p&gt;&amp;lt;&lt;span class="tag"&gt;location&lt;/span&gt;&lt;span class="attr"&gt; path=&lt;/span&gt;&lt;span class="attrv"&gt;&amp;quot;groups.aspx&amp;quot;&lt;/span&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;span class="tag"&gt;system.web&lt;/span&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;span class="tag"&gt;authorization&lt;/span&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;span class="tag"&gt;allow&lt;/span&gt;&lt;span class="attr"&gt; roles=&lt;/span&gt;&lt;span class="attrv"&gt;&amp;quot;mydomain\mygroupname&amp;quot;&lt;/span&gt;/&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;span class="tag"&gt;deny&lt;/span&gt;&lt;span class="attr"&gt; users=&lt;/span&gt;&lt;span class="attrv"&gt;&amp;quot;*&amp;quot;&lt;/span&gt; /&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;span class="tag"&gt;authorization&lt;/span&gt;&amp;gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;span class="tag"&gt;system.web&lt;/span&gt;&amp;gt;&lt;br /&gt;&amp;nbsp; &amp;lt;/&lt;span class="tag"&gt;location&lt;/span&gt;&amp;gt;&lt;/p&gt;
&lt;p&gt;If you choose to not use custom roles,&amp;nbsp;then you may want to consider adding this to your web.config...&lt;/p&gt;
&lt;p&gt;&lt;font color="#0000ff" size="2"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515" size="2"&gt;roleManager&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt; &lt;/font&gt;&lt;font color="#ff0000" size="2"&gt;enabled&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;=&lt;/font&gt;&lt;font size="2"&gt;&amp;quot;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;true&lt;/font&gt;&lt;font size="2"&gt;&amp;quot;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt; &lt;/font&gt;&lt;font color="#ff0000" size="2"&gt;defaultProvider&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;=&lt;/font&gt;&lt;font size="2"&gt;&amp;quot;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;AspNetWindowsTokenRoleProvider&lt;/font&gt;&lt;font size="2"&gt;&amp;quot;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;/&amp;gt;&lt;/p&gt;&lt;/font&gt;
&lt;p&gt;The trick&amp;nbsp;is getting those roles assigned to the current logged in windows user.&lt;/p&gt;
&lt;p&gt;To do this, I create a Global Application Class (global.asax) and within the &lt;font size="2"&gt;Application_PostAuthenticateRequest() event I have code that does the work necessary to build an ArrayList of Roles that the user should be in. &lt;/font&gt;&lt;/p&gt;
&lt;p&gt;Once I have the ArrayList (named &amp;quot;UserRoles&amp;quot;)&amp;nbsp;populated with the names of the&amp;nbsp;roles I want to assign to the current user, I&amp;nbsp;cre&lt;font size="2"&gt;ate a GenericPrinciple and assign it to the Context.User and System.Threading.Thread.CurrentPrinciple, as shown below:&lt;/font&gt;&lt;/p&gt;&lt;font size="2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="cmt"&gt;&amp;#39; Convert ArrayList of Objects into an Array of Strings&lt;br /&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="kwd"&gt;ReDim&lt;/span&gt; aryRoles(UserRoles.Count)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; UserRoles.CopyTo(aryRoles)&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span class="cmt"&gt;&amp;#39; Assign new principle to the system security Context and Thread for THIS user&lt;br /&gt;&lt;/span&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Context.User = &lt;span class="kwd"&gt;New&lt;/span&gt; System.Security.Principal.GenericPrincipal(Context.User.Identity, aryRoles)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; System.Threading.Thread.CurrentPrincipal = &lt;span class="kwd"&gt;New&lt;/span&gt; System.Security.Principal.GenericPrincipal(Context.User.Identity, aryRoles)&lt;br /&gt;&lt;/font&gt;&lt;font size="2"&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Note: I also make sure to put the windows groups the user is currently in, into that list or roles as well.&lt;/p&gt;
&lt;p&gt;Here&amp;#39;s a way to get the names of the roles a user is in... (this is way faster than Active Directory)&lt;/p&gt;&lt;pre class="coloredcode"&gt;        &lt;span class="kwd"&gt;Dim&lt;/span&gt; sb &lt;span class="kwd"&gt;As&lt;/span&gt; StringBuilder = &lt;span class="kwd"&gt;New&lt;/span&gt; StringBuilder
        &lt;span class="kwd"&gt;Dim&lt;/span&gt; WinId &lt;span class="kwd"&gt;As&lt;/span&gt; IIdentity = User.Identity
        &lt;span class="kwd"&gt;Dim&lt;/span&gt; userId &lt;span class="kwd"&gt;As&lt;/span&gt; WindowsIdentity = &lt;span class="kwd"&gt;DirectCast&lt;/span&gt;(WinId, WindowsIdentity)
        sb.Append(&lt;span class="st"&gt;&amp;quot;&amp;lt;br /&amp;gt;&amp;lt;b&amp;gt;Windows Groups&amp;lt;/b&amp;gt; for: &amp;quot;&lt;/span&gt; &amp;amp; userId.Name)
        &lt;span class="kwd"&gt;Dim&lt;/span&gt; irefGroups &lt;span class="kwd"&gt;As&lt;/span&gt; IdentityReferenceCollection = userId.Groups.Translate(&lt;span class="kwd"&gt;GetType&lt;/span&gt;(NTAccount))
        &lt;span class="kwd"&gt;Dim&lt;/span&gt; idRef &lt;span class="kwd"&gt;As&lt;/span&gt; NTAccount
        &lt;span class="kwd"&gt;For&lt;/span&gt; i &lt;span class="kwd"&gt;As Integer&lt;/span&gt; = 0 &lt;span class="kwd"&gt;To&lt;/span&gt; irefGroups.Count - 1
            idRef = irefGroups(i)
            sb.Append(&lt;span class="st"&gt;&amp;quot;&amp;lt;br /&amp;gt;&amp;quot;&lt;/span&gt; &amp;amp; idRef.ToString)
        &lt;span class="kwd"&gt;Next&lt;/span&gt;
        Label1.Text = sb.ToString()&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I know, my method is a little complex. But I do it because my intranet&amp;nbsp;web applications are &amp;quot;products&amp;quot; that I sell to clients. They set up their sites and they are not all that technical. So I need to be able to provide them a web interface that they can use to authorize users easily either by&amp;nbsp;the Windows User Account name (domain\user) or Windows Group name (domain\group). With thousands of possible users, Forms authentication was out of the question. (What&amp;nbsp;a nightmare for a non-technical person to have to deal with). In my method, they can utilize pre-existing windows groups and be up and running quickly.&lt;/p&gt;
&lt;p&gt;Hope this helps.&lt;/font&gt;&lt;/p&gt;</description></item><item><title>Associate Business Logic to Current User/Does it still make sence to Extend User and IPrinciple in 2.0</title><link>http://forums.asp.net/thread/1995646.aspx</link><pubDate>Wed, 07 Nov 2007 05:00:00 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:1995646</guid><dc:creator>dwilliams459</dc:creator><description>&lt;p&gt;I am looking for the best way associate business objects to the current user of the website.&amp;nbsp; I&amp;nbsp;am guessing the best way to do this is to&amp;nbsp;extend the Identity.User object (i.e. extend the user object to include properties such as&amp;nbsp;User.ID, User.Company. etc.).&amp;nbsp; A search for a solution reveals&amp;nbsp;a number of discussions and articles about extending IPrinciple etc, but most of these seems old and related to 1.1.&amp;nbsp; I do not know if this is still the best method in 2.0.&amp;nbsp; I utilized P&amp;amp;P IPrinciple in the 1.1 days, but have not looked at it since.&amp;nbsp; Can someone please let me know if extending IPrinciple is still the best way to map your business objects to your current user?&amp;nbsp; If not can you please&amp;nbsp;point me in a better direction?&amp;nbsp; In either&amp;nbsp;case can you please&amp;nbsp;provide a link to a 2.0&amp;nbsp;based article&amp;nbsp;if you know of one?&amp;nbsp; &lt;/p&gt;
&lt;p&gt;Thanks very much.&amp;nbsp; I am a little rusty when it comes to security and 2.0&lt;/p&gt;</description></item><item><title>Defaultnetworkcredential are empty </title><link>http://forums.asp.net/thread/1852829.aspx</link><pubDate>Fri, 10 Aug 2007 04:00:00 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:1852829</guid><dc:creator>Groslot</dc:creator><description>&lt;p&gt;I have a web apps on a win 2003 server that access a web services on a different web server 2003. When everything is on the same machine it works but when I split them I got a HTTP status 401: Unauthorized. I have impersonate set to true and using &amp;nbsp; &amp;lt;authentication mode=&amp;quot;Windows&amp;quot;/&amp;gt;. The apps has its own web site and owns application pool running with a domain account identity. I have done the delegation (setspn -a http/servername&amp;nbsp;domainname\myaccount)&amp;nbsp;&amp;nbsp;of the account and I have also done the aspnet_regiis - ga&amp;nbsp;domainname\myaccount.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;If fact I have two apps, one is working and the other one not. They both work with basic authentication. It is really a kerberos security contaxt not transfer. The only different between the 2 appps is one we use the &amp;quot;add web reference&amp;quot; which create the wdsl stuff and the other one is the manuual way of doing it by using the SoapHttpClientProtocol. At the webservice server it seen the failing apps present itself with no credential because in the event log I have fail logon mentioning bad username or password and the username field is empty. &lt;/p&gt;
&lt;p&gt;&amp;nbsp;here the stack trace:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;[WebException: The request failed with HTTP status 401: Unauthorized.]&lt;br /&gt;&amp;nbsp;&amp;nbsp; System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage message, WebResponse response, Stream responseStream, Boolean asyncCall) +533199&lt;br /&gt;&amp;nbsp;&amp;nbsp; System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters) +204&lt;br /&gt;&amp;nbsp;&amp;nbsp; NPG.Framework.EntityWSProxy.EntityWSProxy.InvokeWebMethod(String name, Object[] parameters) +664&lt;br /&gt;&amp;nbsp;&amp;nbsp; CSA.MDB.EntityWSProxy.EntityWSProxy.GetMissionBrowserPages(String fileName) +79&lt;br /&gt;&amp;nbsp;&amp;nbsp; CSA.MDB.WebIntranet.TestWS.Button1_Click(Object sender, EventArgs e) +506&lt;br /&gt;&amp;nbsp;&amp;nbsp; System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105&lt;br /&gt;&amp;nbsp;&amp;nbsp; System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107&lt;br /&gt;&amp;nbsp;&amp;nbsp; System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7&lt;br /&gt;&amp;nbsp;&amp;nbsp; System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11&lt;br /&gt;&amp;nbsp;&amp;nbsp; System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33&lt;br /&gt;&amp;nbsp;&amp;nbsp; System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;Anyone has hint for me. I&amp;#39;m not a programmer so be indulgent. I&amp;#39;m a system admin helping our programmers.&lt;/p&gt;</description></item><item><title>Re: PageMethods</title><link>http://forums.asp.net/thread/1438896.aspx</link><pubDate>Tue, 24 Oct 2006 04:00:00 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:1438896</guid><dc:creator>Fredrik K</dc:creator><description>&lt;p&gt;&lt;strong&gt;Page.User&lt;/strong&gt; is merely a convenience property that wraps &lt;strong&gt;System.Threading.Thread.CurrentPrincipal&lt;/strong&gt;, which is static, so just access that instead :)&lt;/p&gt;</description></item><item><title>SerializationException when Casting Context.User to my Custom Principal object</title><link>http://forums.asp.net/thread/1393631.aspx</link><pubDate>Fri, 08 Sep 2006 04:00:00 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:1393631</guid><dc:creator>sontek</dc:creator><description>&lt;p&gt;I&amp;#39;m getting this error randomly on my site:&amp;nbsp;&lt;/p&gt;&lt;p&gt; Exception Details: System.Runtime.Serialization.SerializationException: Type is not resolved for member &amp;#39;Authentication.CustomPrincipal,OrchidAuthentication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null&amp;#39;.&lt;/p&gt;&lt;p&gt;&amp;nbsp;By random meaning I can surf around&amp;nbsp; before it&amp;#39;ll crash and then most of the time I can hit refresh and it&amp;#39;ll work just fine again.&amp;nbsp;&amp;nbsp; I have an HttpModule that sets the context.User to my principal on begin request.&lt;br /&gt;&amp;nbsp;&lt;/p&gt;</description></item></channel></rss>