简体   繁体   English

如何获得WCF Claim / SecurityIdentifier(SID)的WindowsIdentity或WindowsPrincipal?

[英]How can i get the WindowsIdentity or WindowsPrincipal of a WCF Claim / SecurityIdentifier (SID)?

I'm trying to allow all users in the Administrators group access through WCF. 我正在尝试允许管理员组中的所有用户通过WCF进行访问。

internal sealed class AuthorizationManager : ServiceAuthorizationManager
{
    public override bool CheckAccess(OperationContext operationContext)
    {
        base.CheckAccess(operationContext);

        ReadOnlyCollection<ClaimSet> claimSets = operationContext.ServiceSecurityContext.AuthorizationContext.ClaimSets;
        ClaimSet claimSet = claimSets[0];

        foreach (var claim in claimSet.FindClaims(ClaimTypes.Sid, Rights.Identity))
        {
            SecurityIdentifier sid = (SecurityIdentifier)claim.Resource;
            NTAccount ntAccount = (NTAccount)sid.Translate(typeof(NTAccount));

            //This line throws an error.  How can i convert a SecurityIdentifier to a WindowsIdentity?
            WindowsIdentity user = new WindowsIdentity(ntAccount.Value);

            WindowsPrincipal principal = new WindowsPrincipal(user);
            return principal.IsInRole(WindowsBuiltInRole.Administrator);
        }
    }
}

You have to authenticate. 你必须进行身份验证。 You have an identifier that identifies an account, it's isomorphic with an account name ie SID: S-1-5-domain-500 <=> DOMAIN\\Administrator. 您有一个标识帐户的标识符,它与帐户名称同构,即SID:S-1-5-domain-500 <=> DOMAIN \\ Administrator。 A WindowsIdentity is a user that has been authenticated. WindowsIdentity是已经过身份验证的用户。

That said, I think the user you're trying to get has already been authenticated and is providing a claim of his/her account identity (SID). 也就是说,我认为您尝试获取的用户已经过身份验证,并提供了他/她的帐户身份(SID)声明。

JP is correct. JP是对的。 The claims provided include the SID of all user groups the user is a member of. 提供的声明包括用户所属的所有用户组的SID。 Here is our solution. 这是我们的解决方案。

internal sealed class AuthorizationManager : ServiceAuthorizationManager
{
    public override bool CheckAccess(OperationContext operationContext)
    {
        base.CheckAccess(operationContext);

        ReadOnlyCollection<ClaimSet> claimSets = operationContext.ServiceSecurityContext.AuthorizationContext.ClaimSets;
        ClaimSet claimSet = claimSets[0];

            //is this a member of the local admins group
            SecurityIdentifier adminsSid = new SecurityIdentifier("S-1-5-32-544");
            foreach (var claim in claimSet.FindClaims(ClaimTypes.Sid, Rights.PossessProperty))
            {
                if (adminsSid.Equals(claim.Resource))
                {
                    return true;
                }
            }
    }
}

We are using a code we found here to create a WindowsIdentity based on the login name. 我们使用此处找到的代码根据登录名创建WindowsIdentity With a minor modification you can create a similar method that returns a WindowsIdentity based on the SID: 通过一个小修改,您可以创建一个类似的方法,返回基于SID的WindowsIdentity

public static WindowsIdentity GetWindowsIdentityBySid(string sid)
{
    using (var user =
        UserPrincipal.FindByIdentity(
        UserPrincipal.Current.Context,
        IdentityType.Sid,
        sid
        ))
    {
        return user == null
            ? null
            : new WindowsIdentity(user.UserPrincipalName);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过WCF回调发送客户端的WindowsIdentity或WindowsPrincipal / IPrincipal? - Send A Client's WindowsIdentity or WindowsPrincipal/IPrincipal Via WCF Callback? 如何告诉WCF客户端代理类使用Windows身份验证和已登录域用户的WindowsPrincipal作为凭据? - How do I tell a WCF client proxy class to use windows authentication and the WindowsPrincipal of the already logged in domain user as credentials? 如何为远程登录用户获取WindowsIdentity? - How to get WindowsIdentity for a remote logged in user? 为什么在使用消息安全性时会得到WindowsIdentity? - Why do I get a WindowsIdentity when using message security? 如何获取WCF服务正在侦听的端口? - How can I get the port that a WCF service is listening on? 如何获得WCF服务的侦听地址/端口? - How can I get the listening address/port of a WCF service? 如何使用TcpClient类在WCF中获得超时异常 - How can I get a timeout exception in WCF using TcpClient Class 如何调用我的WCF服务构造函数? - How can I get my WCF Service constructor called? 如何在WCF流模式下获取Stream上的Seek方法? - How can I get Seek method on Stream in WCF streaming mode? 如何使用简单的 WCF 应用程序获得 100% CPU 饱和度? - How can I get 100% CPU saturation with simple WCF app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM