Sharepoint 2013 - 如何注销WinRT客户端

时间:2015-10-20 09:00:13

标签: c# sharepoint windows-runtime httpclient logout

我们已经编写了一个连接到Sharepoint 2013的WinRT应用程序。 我们可以对共享点进行身份验证和登录,但我们在注销过程中遇到问题'。登录实现如下:

我们正在使用相应的用户凭据和域信息设置HttpClient。配置包装在HttpClientConfig类中,并传递给保存HttpClient对象的HttpClientService。 之后,我们从sharepoint检索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专家提供的有关如何解决此问题的一些提示或建议。

1 个答案:

答案 0 :(得分:0)

我猜您正在为Windows 10创建一个通用应用。在这种情况下,除了重新启动应用程序see this answer之外别无选择。

HTTP凭据与Cookie不同,因此重置Cookie无济于事。

但是,如果您在Windows 8 / 8.1项目中使用System.Net.Http.HttpClient(没有通用项目),则处置HttpClient应该有效。

Windows 8 / 8.1模板的示例。请勿使用通用模板。

private async void Foo()
{
    // Succeeds, correct username and password.
    await Foo("foo", "bar");

    // Fails, wrong username and passord.
    await Foo("fizz", "buzz");
}

private async Task Foo(string user, string password)
{
    Uri uri = new Uri("http://heyhttp.org/?basic=1&user=foo&password=bar");
    HttpClientHandler handler = new HttpClientHandler();
    handler.Credentials = new System.Net.NetworkCredential(user, password);
    HttpClient client = new HttpClient(handler);
    Debug.WriteLine(await client.GetAsync(uri));
}