"未授权" Simple.OData.Client通过使用Xamarin PCL

时间:2015-05-05 15:25:52

标签: xamarin sharepoint-api simple.odata

在我的 Xamarin 项目中使用 Simple.OData.Client 之前,我在 LINQPad 的帮助下尝试了它。易于使用它给我留下了深刻的印象。当我将它构建到我们的 Xamarin 项目中时,我通过尝试从 SharePoint 的REST Api获取数据而获得异常。

  

Simple.OData.Client.WebRequestException:意外的WebException   遇到---> System.Net.WebException:错误:SendFailure(错误   写标题)---> System.Net.WebException:写入标头时出错   ---> System.IO.IOException:authentication或decr ... {Simple.OData.Client.WebRequestException:Unexpected   遇到WebException ---> System.Net.WebException:错误:   SendFailure(编写标题时出错)---> System.Net.WebException:   写标题时出错---> System.IO.IOException:身份验证   或解密失败。 --->   Mono.Security.Protocol.Tls.TlsException:收到无效的证书   从服务器。错误代码:0xffffffff800b010a

我相信,此异常是由于我们的 SharePoint 实例使用自签名证书。我尝试通过返回 ServerCertificateValidationCallback

<
System.Net.ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true;

现在我从Simple.OData.Client

获取 Unauthorized 异常的所有时间
  

Simple.OData.Client.WebRequestException:未经授权

首次来自 MainViewModel 通过业务层

    private async void InitializeAsync()
    {
        // TODO [Anton Kalcik - Dienstag, 05. Mai 2015 17:09:55]: Show loading indicator
        TaskEntity getTaskForCurrentMonthAsyncTask = await _taksBusinessLayer.GetTaskForCurrentMonthAsync();
        _timeToDisplay = getTaskForCurrentMonthAsyncTask.DueDate - DateTime.Now;
        // TODO [Anton Kalcik - Dienstag, 05. Mai 2015 17:10:04]: Hide loading indicator

        StartCountdownTimer();
    }

执行调用的类是类 SharePointTaskRepository

public class SharePointTaskRepository : ITaskRepository
{
    private readonly string _collectionName;
    private readonly ODataClient _oDataClient;

    public SharePointTaskRepository(Uri sharepointUri, string collectionName, ICredentials credentials)
    {
        if (sharepointUri == null)
        {
            throw new ArgumentNullException("sharepointUri");
        }
        if (String.IsNullOrWhiteSpace(collectionName))
        {
            throw new ArgumentException("Argument can't be null, empty or white space!", "collectionName");
        }
        if (credentials == null)
        {
            throw new ArgumentNullException("credentials");
        }

        _collectionName = collectionName;

        var oDataClientSettings = new ODataClientSettings(sharepointUri, credentials);
        _oDataClient = new ODataClient(oDataClientSettings);
    }

    public async Task<IEnumerable<TaskModel>> ReadAsync(Expression<Func<TaskModel, bool>> filter, Expression<Func<TaskModel, object>> orderBy, int numberOfResults)
    {
        return await _oDataClient
                    .For<TaskModel>(_collectionName)
                    .Filter(filter)
                    .OrderBy(orderBy)
                    .Top(numberOfResults)
                    .FindEntriesAsync();
    }
}

我仔细检查了凭证,这绝对是正确的。利用 ServerCertificateValidationCallback 的代码位于 ApplicationRuntimeSettings 中。此类是特定于平台的单例,并通过依赖注入作为所有其他对象提供。

[assembly: Dependency(typeof(ApplicationRuntimeSettings))]
namespace AZeitReminder.Droid.Infrastructure
{
    public class ApplicationRuntimeSettings : ApplicationRuntimeSettingsBase
    {
        public ApplicationRuntimeSettings()
        {
            System.Net.ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true;
        }

        public override SQLiteConnection CreateSqLiteConnection()
        {
                string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
                var path = Path.Combine(documentsPath, DatabaseFileName);
                var currentPlatform = new SQLitePlatformAndroid();
                var connection = new SQLiteConnection(currentPlatform, path);

                return connection;
        }

        public override CultureInfo GetCultureInfo()
        {
            var androidLocale = Java.Util.Locale.Default;
            var netLanguage = androidLocale.ToString().Replace("_", "-"); // NOTE [Anton Kalcik - Dienstag, 05. Mai 2015 17:21:10]: turns pt_BR into pt-BR
            return new CultureInfo(netLanguage);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您可以尝试设置PreAuthenticate = false;为了您的要求。 Simple.ODataClient在内部使用HttpClientHandler。这个HttpClientHandler设置PreAuthenticate = true;但您可以在OnApplyClientHandler中修改此处理程序并将该属性设置为false。在您的代码中尝试此操作:

oDataClientSettings.OnApplyClientHandler = handler => handler.PreAuthenticate = false;

原因是你的Sharepoint服务器可以抛出“Unauthorized”作为质询响应,而WebRequest不会回答质询,除非此属性为false。