0

我想使用控制台应用程序获取用户在 SharePoint 中所属的所有组的列表。我是 SharePoint 开发新手,所以这是我公认的业余尝试。

    private static string GetUserGroupCollection(string LoginName)
    {

        UserGroupSoapClient ug = new UserGroupSoapClient();
        ug.ClientCredentials.Windows.ClientCredential.UserName = "myusername";
        ug.ClientCredentials.Windows.ClientCredential.Password = "mypassword";
        return ( ug.GetGroupCollectionFromUser(LoginName)).ToString();
    }

我已在我的 SP Web 服务中包含“服务参考”,网址为 http://server02/aaa/DOCS/_vti_bin/usergroup.asmx

当我尝试上面的代码时,我得到The HTTP request was forbidden with client authentication scheme 'Anonymous'.

你能告诉我一个如何做到这一点的基本例子吗?请注意,我不想将引用作为“Web 引用”。谢谢你。

4

2 回答 2

3

如果您的 SharePoint WebServices Web 应用程序使用 NTLM 身份验证,您可以试试这个

ug.ClientCredentials.Windows.ClientCredential = new NetworkCredential("myusername", "mypassword");

在你的app.config

<security mode="Transport">
    <transport clientCredentialType="Ntlm" proxyCredentialType="None" realm="" />
    <message clientCredentialType="UserName" algorithmSuite="Default" />
</security>

编辑:

由于在您的 Web 应用程序中禁用 NTLM 身份验证以访问 Web 服务,您必须首先使用 Authentication.asmx Web 服务登录并检索身份验证 cookie 并将其与其他 Web 服务调用一起发送,如下所示:

            string cookie = "";
        LoginResult loginResult;
        string result;

        AuthenticationSoapClient authentication = new AuthenticationSoapClient();
        UserGroupSoapClient ug = new UserGroupSoapClient();

        using(OperationContextScope scope = new OperationContextScope(authentication.InnerChannel))
        {
            loginResult = authentication.Login("user", "pass");
            if (loginResult.ErrorCode == LoginErrorCode.NoError)
            {
                MessageProperties props = OperationContext.Current.IncomingMessageProperties;
                HttpResponseMessageProperty prop = props[HttpResponseMessageProperty.Name] as HttpResponseMessageProperty;
                string cookies = prop.Headers["Set-Cookie"];
                // You must search cookies to find cookie named loginResult.CookieName and set its value to cookie variable;
                cookie = cookies.Split(';').SingleOrDefault(c => c.StartsWith(loginResult.CookieName));
            }
        }

        HttpRequestMessageProperty httpRequestProperty = new HttpRequestMessageProperty();
        httpRequestProperty.Headers.Add(System.Net.HttpRequestHeader.Cookie, cookie);
        using (new OperationContextScope(ug.InnerChannel))
        {
            OperationContext.Current.OutgoingMessageProperties.Add(HttpRequestMessageProperty.Name, httpRequestProperty);
            result = ug.GetGroupCollectionFromUser(LoginName).ToString();
        }

并确保 app.config 中所有绑定的 allowCookies 属性为 false。

<basicHttpBinding>
   <binding name="AuthenticationSoap" allowCookies="false"/>
   <binding name="UserGroupSoap" allowCookies="false"/>
</basicHttpBinding>
于 2013-04-04T06:43:53.823 回答
1

类似以下的内容应该有效:(取自/编辑自http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/b17ae5c8-f845-4cee-8298-c4f7b5c52b57

我认为您应该使用 SPGroup 从站点获取所有组并在所有组中找到用户:

对于单个用户,如果您知道组名,那么您可以尝试使用以下代码:

SPWeb site = SPContext.Current.Web;
SPGroup groupToQuery = site.Groups["Project"];
bool istrue = site.IsCurrentUserMemberOfGroup(groupToQuery);

如果您不知道组名,请尝试使用以下代码(我没有测试过)

using (SPWeb rootWeb = topLevelSite.OpenWeb())
{
    foreach (SPGroup group in rootWeb.Groups)
    {
        bool isUserInGroup = IsUserInGroup(group, currentUser.LoginName);
    }
}

private Boolean IsUserInGroup(SPGroup group, string userLoginName)
{
    bool userIsInGroup = false;

    try
    {
        SPUser x = group.Users[userLoginName];
        userIsInGroup = true;
    }
    catch (SPException)
    {
        userIsInGroup = false;
    }

    return userIsInGroup;
}
于 2013-04-02T13:10:52.393 回答