在我们在我们的 xamarin 项目中使用 simple.odata.client 之前,我在 linqpad的帮助下尝试了它em>。使用它的简单性给我留下了深刻的印象。当我将其构建到我们的 Xamarin 项目中时,我尝试从 sharepoint 的REST API中获取数据来获得异常。
simple.odata.client.webrequestexception:意外的WebException 遇到的---> system.net.webexception:错误:sendfailure(错误) 编写标题)---> System.Net.WebException:错误写入标题 ---> system.io.ioexception:身份验证或def ... {simple.odata.client.webrequestexception:出乎意料 遇到的WebException ----> System.net.webexception:错误: sendfailure(错误编写标题)---> System.net.webexception: 错误编写标题---> system.io.io.ioexception:身份验证 或解密失败。---> mono.security.protocol.tls.tlsexception:收到的无效证书 从服务器。错误代码:0xFFFFFFFFFFFFFFFFFFF5800B010A
我相信,这个例外是由于我们的 sharepoint 实例使用自签名证书引起的。我尝试通过始终返回 serverCertificateValidationCallback
来消除它来消除它。System.Net.ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true;
现在,我所有的时间都从simple.odata.client
中获得未授权的例外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);
}
}
}
您可以尝试设置prateatenticate = false;出于您的要求。simple.odataclient在内部使用httpclienthandler。此httpclienthandler设置prateatenticate = true;但是您可以在onapplyclienthandler中修改此处理程序,并将属性设置为false。在您的代码中尝试一下:
oDataClientSettings.OnApplyClientHandler = handler => handler.PreAuthenticate = false;
原因是您的SharePoint服务器可以将"未经授权"作为挑战响应,除非此属性为false,否则WebRequest不会回答挑战。