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();
    }

で利用可能なSPWebサービスへの「サービスリファレンス」を含めました。 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アプリケーションではWebサービスにアクセスするためにNTLM認証が無効になっているため、最初に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 に答える