Click here to Skip to main content
15,898,628 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My application has basic authentication and runs under Application pool Integrated-Network service.
Web Config file has list of domain groups (office\admin,office\sales), defined which has access to the some file resource.
How do i check if user belongs to those groups or not and decide if he can read access those resource.




If you look down at my code,
line
var userGroups = context.Request.LogonUserIdentity.Groups;


returns {s-#-##-###} kind of group.

and line below
hasPermission = userGroups.Contains(account.Translate(typeof(SecurityIdentifier)));//throws exception


An exception of type 'System.Security.Principal.IdentityNotMappedException' occurred in mscorlib.dll but was not handled in user code

Additional information: Some or all identity references could not be translated.


What I have tried:

public void ProcessRequest(HttpContext context)
      {
          //Get a collection of Groups the user belongs to
          var userGroups = context.Request.LogonUserIdentity.Groups;

          if (userGroups.Count > 0)
          {
              if (HasPermision(userGroups))
              {
                  string urlRequested = context.Request.RawUrl.ToLower();
                  string fileName = Path.GetFileName(urlRequested);
                  string fileServer = collection["FileServer"];
                  var filePath = (fileServer + urlRequested.Replace("/", "\\"));
                  var fileExtension = urlRequested.Substring(urlRequested.LastIndexOf(".", System.StringComparison.Ordinal) + 1);

                  try
                  {
                      context.Response.Write("do some works...");
                  }
                  catch (Exception ex)
                  {
                      throw new Exception(ex.Message);
                  }
              }
          }
          else
          {
              context.Response.StatusCode = 403;
              context.Response.Flush();
          }
      }

      public bool HasPermision(IdentityReferenceCollection userGroups)
      {
          bool hasPermission = false;

          //The security group you want to check the user belongs to
          NTAccount account;

          // get authorized groups from config files
          string[] authorizedGroups = collection["AuthorizedGroups"].Replace(" ", "").ToUpper().Split(',');

          foreach (var group in authorizedGroups)
          {
              account = new NTAccount(group);

              // Check if user is in the groups
              hasPermission = userGroups.Contains(account.Translate(typeof(SecurityIdentifier)));//throws exception
              if (hasPermission)
                  return hasPermission;
          }
          return hasPermission;
      }
Posted
Updated 20-Dec-17 10:43am

1 solution

Get the page user, the below code works fine for me;
C#
System.Security.Principal.WindowsPrincipal pageUser = User as System.Security.Principal.WindowsPrincipal;


Check if the User is in a specific security group;
C#
if(pageUser.IsInRole(@"Domain\Security Group Name"))
{
    // User is in the security group
}
else
{
    // user is not in the security group
}

Works for Domain or Local Machine accounts

Kind Regards
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900