【发布时间】:2015-10-20 09:00:13
【问题描述】:
我们编写了一个连接到 Sharepoint 2013 的 WinRT 应用程序。 我们能够验证并登录到共享点,但我们在注销“过程”时遇到了问题。登录实现如下:
我们正在使用相应的用户凭据和域信息设置一个 HttpClient。配置包装在 HttpClientConfig 类中,并传递给持有 HttpClient 对象的 HttpClientService。
之后,我们从共享点检索formdigestValue,并在每个请求中使用 X-RequestDigest 标头中的令牌。如果令牌超时,我们会检索一个新令牌。
这是我们如何实现上述身份验证的一些代码。
public async Task Inialize()
{
var httpConfig = new HttpClientConfig();
httpConfig.Headers.Add("Accept", "application/json;odata=verbose");
httpConfig.Headers.Add("User-Agent", _userAgent);
httpConfig.DefaultTimeout = Statics.DEFAULT_NETWORK_TIMEOUT_SECONDS;
httpConfig.PreAuthenticate = true;
httpConfig.NetworkCredentials = new NetworkCredential(username, password, _domain);
_httpClientService.ResetCookies();
_httpClientService.ConfigureHttpClient(httpConfig);
}
ConfigureHttpClient 方法处理旧的 HttpClient 实例并创建一个新的 HttpClient 实例,如下所示:
public void ConfigureHttpClient(HttpClientConfig config, bool disposeCurrent = true)
{
_config = config;
if (disposeCurrent)
{
DisposeHttpClient();
}
_httpClient = CreateHttpClient(config);
if (disposeCurrent)
{
//make sure remove old httpclient and httpclienthandler instances after they are not hold anywhere else
GC.Collect();
}
_httpClientDisposed = false;
}
public HttpClient CreateHttpClient(HttpClientConfig config)
{
_httpClientHandler = _httpClientFactoryService.CreateHttpClientHandler();
_httpClientHandler.CookieContainer = _cookieContainer;
_httpClientHandler.UseCookies = true;
_httpClientHandler.AllowAutoRedirect = config.AllowAutoRedirect;
_httpClientHandler.PreAuthenticate = config.PreAuthenticate;
if (config.NetworkCredentials != null)
{
_httpClientHandler.Credentials = config.NetworkCredentials;
}
var client = _httpClientFactoryService.CreateHttpClient(_httpClientHandler, true);
client.Timeout = TimeSpan.FromSeconds(config.DefaultTimeout);
if (config.UseGzipCompression)
{
if (_httpClientHandler.SupportsAutomaticDecompression)
{
_httpClientHandler.AutomaticDecompression = DecompressionMethods.GZip;
client.DefaultRequestHeaders.AcceptEncoding.Add(StringWithQualityHeaderValue.Parse("gzip"));
}
}
return client;
}
public void DisposeHttpClient()
{
var client = _httpClient;
_httpClientDisposed = true; //set flag before disposing is done to be able to react correctly!
if (client != null)
{
client.Dispose();
}
var handler = _httpClientHandler;
if (handler != null)
{
handler.Dispose();
}
GC.Collect();
}
public async Task<object> InitNewSharepointSession(bool useCookies = true)
{
var config = _httpClientService.CurrentClientConfig;
config.UseCookies = useCookies;
var res = await getRequestDigestAsync();
if (res.IsSuccess)
{
SharepointContextInformation = res.Response;
if (config.Headers.ContainsKey("X-RequestDigest"))
{
config.Headers.Remove("X-RequestDigest");
}
config.Headers.Add("X-RequestDigest", SharepointContextInformation.FormDigestValue);
return new DataServiceResponse<bool>(true);
}
else
{
return new DataServiceResponse<bool>(res.Error);
}
}
ResetCookies 方法只处理旧的 cookie 列表:
public void ResetCookies()
{
_cookieContainer = new CookieContainer();
}
如您所见,我们使用了一些 GC.Collect() 调用,根据注销的内容,这表明我们有点无助。 对于注销,我们只需处理我们的 httpclient。 但是由于某种原因,如果我们用另一个用户登录,我们有时会得到前一个用户的数据,这对我们来说是一个相当高的错误。 如果我们重新启动应用程序一切正常,但如果我们只处理当前用户 httpClient,我们可能会在此失败中运行,并使用前一个用户的错误凭据/用户上下文进行访问。
我观察到的另一件事是密码更改后的行为。旧密码保持有效,直到应用重新启动。
因此,我非常感谢 sharepoint REST 专家就如何解决此问题提供一些提示或建议。
【问题讨论】:
标签: c# sharepoint windows-runtime httpclient logout