我有一种情况,我需要确认我能够获得对网络上特定文件夹的写入权限。因此,首先我要检查的是用户或其中一个组是其中一个组(域组)的访问规则中的身份之一。如果不是这种情况,那么我需要检查并检查远程计算机上的所有本地组,以查看我的用户或其中一个用户组是否是本机上的本地组的成员。例如,在这台特定的机器上,我是BUILTIN \ Administrators的成员,这意味着我具有对给定文件夹的写入权限。但是我不清楚如何从远程机器获取本地组以检查是否有写入权限。通过网络获取文件权限
在下面的代码中,当我尝试使用GroupPrincipal.FindByIdentity时,它会发出异常“绑定句柄无效”。我不清楚什么是无效的。如果我只是尝试使用ctx.ValidateCredentials(UserName,Password)验证我的用户名和密码(域用户名),它会给出完全相同的错误。
如果我将机器名称设置为“pvr-pc”,则表示它无法在网络上找到该名称,但“\\ pvr-pc”确实可以很好地解决该问题。
相同的代码正确地发现我所属的一个组是我的本地计算机上的BUILTIN \ Administrators组中的一个组,因此它是关于网络访问的问题。
有没有人有任何想法我做错了什么?
的代码看起来是这样的:
using (WindowsIdentity identity = GetUserIdentity())
{
if (identity != null)
{
try
{
FileInfo fi = new FileInfo(@"\\pvr-pc\c\installers\");
AuthorizationRuleCollection acl = fi.GetAccessControl().GetAccessRules
(true, true, typeof (SecurityIdentifier));
var rules = acl.Cast<FileSystemAccessRule>();
List<string> sids = new List<string>();
sids.Add(identity.User.Value);
sids.AddRange(identity.Groups.Select(identityReference => identityReference.Value));
// check for a direct user match
var matches = from r in rules where sids.Contains(r.IdentityReference.Value) select r;
foreach (FileSystemAccessRule accessRule in matches)
{
// apply rules
}
foreach (FileSystemAccessRule rule in rules)
{
// if it is built in, try and get the group
var groupDetail = rule.IdentityReference.Translate(typeof (NTAccount));
if (!groupDetail.Value.StartsWith("BUILTIN\\")) continue;
PrincipalContext ctx = new PrincipalContext(ContextType.Machine, @"\\pvr-pc", null,
ContextOptions.Negotiate, UserName, Password);
GroupPrincipal grp = GroupPrincipal.FindByIdentity(ctx, IdentityType.Sid,
rule.IdentityReference.Value);
if (grp != null)
{
//// find out if we are a member of the group
var isInGroup = (from g in grp.GetMembers(true)
where sids.Contains(g.Sid.ToString())
select g).Any();
if (isInGroup)
{
// apply rules
}
}
}
}
catch (Exception ex)
{
}
感谢, 斯特凡
是的,如果我不能这样做,我肯定会这样做。在运行这段代码的时候,我没有任何具体的东西我正在读或写,所以我必须阅读一个随机文件或者创建一个随机文件,如果我选择了一个随机文件,我宁愿不去做可以。 FileInfo为您提供了一个IsReadOnly属性,可能会有所帮助,但尚未进行任何测试,以确定它是否能在所有情况下都能正常工作。 – Stefan 2012-07-13 03:18:51