他のユーザーがサインインした後に現在のユーザーがWebServiceでは変更されません
-
10-12-2019 - |
質問
コンテキスト:私は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;
}
}
対応するサービスファイルUserService.svc
は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がSharpointにサインインしているかどうかは関係ありません。 Webサービスを最初に呼び出したユーザーは、WebサービスまたはJavaScriptコードのいずれかによってキャッシュされているのはsomwhowです。
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);
}
});
}