Question

The context: I have this web service hosted in SharePoint.

[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;
  }
}

The corresponding service file UserService.svc is mapped to the ISAPI folder and looks like this:

<%@ 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$" %>

I am calling the web service from a SharePoint page through JavaScript code.

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

The URL is hard coded for demo purposes only.

The problem: Suppose User A is the one who has first invoked the JavaScript function. Then the service returns his username. If afterwards anyone else signs in to SharePoint and calls the web service, then he will get User A's username. If IE is closed and started new and if the User C is the one who has first called the web service then his username is always returned. It doesn't matter whether User A or User B signs in to SharPoint. It is somwhow that the user who has first called the web service is cached by either the web service or the JavaScript piece of code.

What do I have to do so that the web service returns the right username?

Was it helpful?

Solution

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);
    }
  });
}
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top