Sharepoint 2013-如何注销WinRT客户端 [英] Sharepoint 2013 - How to logout a WinRT Client

查看:107
本文介绍了Sharepoint 2013-如何注销WinRT客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们编写了一个与Sharepoint 2013连接的WinRT应用. 我们可以进行身份​​验证并登录到共享点,但是注销流程"存在问题.登录实现如下:

we have written a WinRT App connected to a Sharepoint 2013. We are able to authenticate and login to the sharepoint, but we have problems with the logout 'process'. Login is implemented as follows:

我们正在使用相应的用户凭据和域信息来设置HttpClient.该配置被包装在HttpClientConfig类中,并传递给保存HttpClient对象的HttpClientService. 之后,我们从共享点检索formdigestValue,并在每个请求中使用X-RequestDigest标头中的令牌.如果令牌超时,我们将获取一个新令牌.

We are setting up a HttpClient with the corresponding user credentials and domain information. The configuration is wrapped in the HttpClientConfig class an delivered to a the HttpClientService which holds the HttpClient object. After that we retrieve the formdigestValue from the sharepoint and use the token in the X-RequestDigest Header in every request. If the token times out we retrieve a new one.

以下是我们实现上述身份验证的一些代码.

Here is some code how we implemented the above mentioned authentication.

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实例,如下所示:

The ConfigureHttpClient method disposes an old HttpClient instance and creates a new HttpClient instance, like this:

    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列表:

The ResetCookies method only disposes the old cookies list:

public void ResetCookies()
        {
            _cookieContainer = new CookieContainer();
        }


如您所见,我们使用了一些GC.Collect()调用,根据注销内容显示了一些无助感. 对于注销,我们只需要配置我们的httpclient即可. 但是由于某种原因,如果我们与另一个用户登录,有时我们会得到前一个用户的数据,这对我们来说是相当高的错误率. 如果重新启动应用程序,一切都会很好,但是如果仅处置当前用户httpClient,则可能会在此故障中运行,并且使用先前用户的错误凭据/用户上下文进行访问.


As you can see we used some GC.Collect() calls which shows a bit our helplessness according the logout stuff. For logout, we just dispose our httpclient. But for some reason, if we login with another user, we sometimes get the data of the previous user which is a pretty high rated bug for us. Everything works nice if we restart the app, but if we only dispose the current users httpClient we may run in this failure having access with the wrong credential/user context of the previous user.

我看到的另一件事是密码更改后的行为.旧密码保持有效,直到重新启动该应用程序为止.

Another thing I watched is the behaviour after a password change. The old password remains and is valid until the app has been restarted.

因此,我非常感谢SharePoint REST专家关于如何解决此问题的一些提示或建议.

So I would be very thankful for some hints or suggestions of a sharepoint REST specialist on how to solve this issue.

推荐答案

我想您正在为Windows 10创建通用应用程序.在这种情况下,除了重新启动应用程序外,没有其他选择,

I guess you are creating a Universal app for Windows 10. In that case, there is no other option than restarting the app, see this answer.

HTTP凭据与cookie不同,因此重置cookie将无济于事.

HTTP credentials are not the same as cookies, so resetting the cookies will not help.

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

However, if you are using System.Net.Http.HttpClient in a Windows 8/8.1 project (no Universal project), disposing the HttpClient should work.

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

这篇关于Sharepoint 2013-如何注销WinRT客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆