上下文:我有sharepoint托管的这个web服务。

[ServiceContract]
public interface IUserService
{
  [WebInvoke(UriTemplate = "/GetCurrentUser", Method = "GET", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json)]
  [OperationContract]
  string GetCurrentUser();
}

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class UserService: IUserService
{
  public string GetCurrentUser()
  {
    return SPContext.Current.Web.CurrentUser.LoginName;
  }
}
.

相应的服务文件生成generacicetagcode映射到ISAPI文件夹,如下所示:

<%@ ServiceHost Factory="Microsoft.SharePoint.Client.Services.MultipleBaseAddressWebServiceHostFactory, Microsoft.SharePoint.Client.ServerRuntime, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"
                Language="C#" Debug="true"
                Service="UserService, $SharePoint.Project.AssemblyFullName$" %>
.

我通过JavaScript代码从SharePoint页面调用Web服务。

function getCurrentUser() {
  $.ajax({
    type: 'GET',
    url: 'http://server/sites/rootweb/_vti_bin/UserService.svc/GetCurrentUser',
    success: function (data) {
      printToDiv(data);
    }
  });
}
.

URL仅用于演示目的。

问题:假设用户A是第一次调用JavaScript函数的人。然后服务返回他的用户名。如果之后,其他人签到SharePoint并调用Web服务,那么他将获得用户A的用户名。如果IE已关闭并启动新功能,如果用户C是第一个称为Web服务的人,则始终返回他的用户名。用户A或用户B是否签署狙击数并不重要。它是通过Web服务或JavaScript代码缓存的第一个称为Web服务的用户。

我要做什么,以便Web服务返回正确的用户名?

有帮助吗?

解决方案

It could be due to the browser caching the response of your AJAX request, IE loves this for example. Try adding: cache:false to the AJAX request:

function getCurrentUser() {
  $.ajax({
    type: 'GET',
    cache: false,
    url: 'http://server/sites/rootweb/_vti_bin/UserService.svc/GetCurrentUser',
    success: function (data) {
      printToDiv(data);
    }
  });
}
许可以下: CC-BY-SA归因
scroll top