SharePoint GetGroupCollectionFromUser Web服务返回“HTTP请求被禁止”错误
本文关键字:请求 禁止 错误 HTTP GetGroupCollectionFromUser Web 服务 返回 SharePoint | 更新日期: 2023-09-27 17:55:03
我想使用控制台应用程序获得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();
}
我在http://server02/aaa/DOCS/_vti_bin/usergroup.asmx
上包含了一个"服务参考"到我的SP web服务
当我尝试上面的代码,我得到The HTTP request was forbidden with client authentication scheme 'Anonymous'.
如果您的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>
编辑:
因为NTLM身份验证在您的web应用程序中被禁用以访问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>
下面的内容应该可以工作:(取自/编辑自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;
}