I need to run a console application that loops through all a server's repositories and then lock each repository's volumes. I have one Windows AD account as admin to all the repositories.
I'v managed to Impersonate and set the WindowsIdentity to this AD account, and server.GetRepositories() is calling successfully. However, when i try session.Login(repository), it's giving me Access Denied.
RepositoryRegistration repository = new RepositoryRegistration(serverName, repoName); session.LogIn(repository);
I even tried setting the Thread's CurrentPrincipal Identity based on that of the WindowsIdentity, and it's still not working. What am i missing?
// Retrieve a GenericPrincipal that is based on the current user's WindowsIdentity.
GenericPrincipal genericPrincipal = GetGenericPrincipal();
// Retrieve the generic identity of the GenericPrincipal object.
GenericIdentity principalIdentity = (GenericIdentity)genericPrincipal.Identity;
Thread.CurrentPrincipal = genericPrincipal;
// Create a generic principal based on values from the current WindowsIdentity.
private static GenericPrincipal GetGenericPrincipal()
{
// Use values from the current WindowsIdentity to construct
// a set of GenericPrincipal roles.
WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent();
string[] roles = new string[10];
if (windowsIdentity.IsAuthenticated)
{
// Add custom NetworkUser role.
roles[0] = "NetworkUser";
}
if (windowsIdentity.IsGuest)
{
// Add custom GuestUser role.
roles[1] = "GuestUser";
}
if (windowsIdentity.IsSystem)
{
// Add custom SystemUser role.
roles[2] = "SystemUser";
}
// Construct a GenericIdentity object based on the current Windows
// identity name and authentication type.
string authenticationType = windowsIdentity.AuthenticationType;
string userName = windowsIdentity.Name;
GenericIdentity genericIdentity =
new GenericIdentity(userName, authenticationType);
// Construct a GenericPrincipal object based on the generic identity
// and custom roles for the user.
GenericPrincipal genericPrincipal =
new GenericPrincipal(genericIdentity, roles);
return genericPrincipal;
}