我有一个小型 C# ASP.Net WebService。我喜欢从 Windows 客户端调用此 WebService 并在 SharePoint 文档库上做一些事情。 (例如上传文件)
但是如果我上传一个文件,它将使用来自网络服务的凭据而不是来自调用 WebService 的用户的凭据来执行。结果 = 所有文件将显示为“创建者” = 系统帐户...
是否可以使用当前的 Windows 用户凭据调用此 WebService(从浏览器或脚本 $.Ajax)并在 WebService 中使用这些凭据?
我知道我可以通过这种方式显示调用 WebService 的用户:
return Username = User.Identity.Name;
但是您不能使用它来处理这些类型的凭据吗?
我已经尝试了几件事,但无法获得当前用户。
代码示例 1:(SharePoint 客户端对象模型)
ClientContext context = new ClientContext("http://domain.local");
context.Credentials = System.Net.CredentialCache.DefaultCredentials;
代码示例 2:(SharePoint 服务器对象模型)
SPUserToken userToken = null;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite("SiteUrl"))
{
using (SPWeb web = site.OpenWeb())
{
userToken = web.AllUsers["DOMAIN\\USERNAME"].UserToken;
}
});
using (SPSite site = new SPSite("SiteUrl", userToken))
{
using (SPWeb web = site.OpenWeb())
{
// Perform actions
}
}
代码示例 3:(我不喜欢那个) 我找到了一种可能的方法,但我必须在代码中输入我不喜欢的密码:
NetworkCredential credentials = new NetworkCredential("username", "pwd", "domain");
ClientContext context = new ClientContext("http://site-url");
context.Credentials = credentials;
...
我觉得web.config文件中最重要的东西也在这里?:
<authentication mode="Windows"/>
<add name="Access-Control-Allow-Origin" value="http://domain.local"/>
<add name="Access-Control-Allow-Credentials" value="true"/>
<add name="Access-Control-Allow-Headers" value="Content-Type"/>
关于如何使用当前用户的用户上下文执行代码有什么想法吗?
脚本调用:
$.ajaxSetup({
type: "GET",
url: "webServiceUrl",
data: { string: "value" },
dataType: 'json',
xhrFields: {
withCredentials: true
},
crossDomain: true,
success: function(data){
},
error: function(XMLHttpRequest, textStatus, errorThrown){
}
});
最佳答案
我不知道你的要求和你的环境,但你不能使用 CSOM 直接从 windows 客户端上传文件吗?
这里有一个关于如何设置约束委派的链接:http://msdn.microsoft.com/en-us/library/ff650591.aspx#_Step_7:_Impersonate .
关于c# - 是否可以使用当前的 Windows 用户凭据调用 c# WebService?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18216899/