2016-05-03 23 views
-3

私はXamarinとモバイル開発の新機能です。現在、認証付きサンプルアプリケーションの開発に携わっています。私たちは内部Web APIサービスを使用して認証しています。私は自分の開発にMvvmCrossフレームワークを使用する予定です。私は最初の開発の一環として、Xamarinフォームを使用せず、Android用のプラットフォーム固有のプロジェクトを使用していません。どのような助け本当に感謝します。ここでxamarin web API認証

は、私は、要求がHTTPを使用しますが

HTTPS

に失敗しますサンプルを呼び出すことができた私は

public async Task<HttpResponseMessage> PutAsync<T>(string url,T content) 
     { 

      using (var client = new HttpClient(new NativeMessageHandler())) 
      // using (var client = new HttpClient()) 
      { 
       string json = JsonConvert.SerializeObject(content); 
       var address = string.Format("{0}{1}", WebService.BaseUrl, url); 
       var httpContent = new StringContent(json, Encoding.UTF8, "application/json"); 
       var response = await client.PutAsync(new System.Uri(address), httpContent); 
       return response; 
      } 
     }`enter code here` 

私のWebAPIのを呼び出すために使用していたサンプルコードです

リクエスト。私はあなたのサービスがOAuthの(私はアプリをお勧めします何を)サポートしている場合、あなたはあなたのユーザーを認証するXamarin.Authライブラリを使用することができますアンドロイド4.4キットカットデバイス

System.Net.Http.HttpMessageHandler handler = new NativeMessageHandler(false,true); 

.Net.Http.HttpClient client = (handler == null) ? new System.Net.Http.HttpClient() :new System.Net.Http.HttpClient(new NativeMessageHandler(false,true)); 
result = client.GetAsync("https://httpbin.org/ip"); 
       var stream = await result.Result.Content.ReadAsStringAsync(); 
+0

私はちょうどテストプロジェクトを使用して自分のPCLクラスをテストするときに動作しますが、Androidデバイスから実行すると応答はありません。 – Biju

+0

応答ができません。何か間違っていることを示す例外や他のものがなければなりません。あなたは 'address =" http:// localhost/... "'を使用していないか、そういうもの、または! –

+0

はい、localhostエンドポイントを指すローカルサービスを使用しています。以下のようになります 携帯電話のhttp:// localhost/xxxxxxx/api/security/login/ – Biju

答えて

0

:ここ

https://www.nuget.org/packages/Xamarin.Auth/は、それを使用する方法のチュートリアルです。

using System.Threading.Tasks; 
using Newtonsoft.Json; 
using ModernHttpClient; 
using System.Net.Http; 
using System.Text; 
namespace App.Portable 
{ 
    public interface IHttpService 
    { 
     Task<T> ReadContentAsync<T>(Task<HttpResponseMessage> response); 
     Task<HttpResponseMessage> GetAsync(string url); 
     Task<string> PutAsync<T>(string url,T content); 
    } 

    public class HttpService : IHttpService 
    { 

     public async Task<HttpResponseMessage> GetAsync(string url) 
     { 
      throw new HttpRequestException(); 
     } 
     public async Task<string> PutAsync<T>(string url,T content) 
     { 
      HttpMessageHandler handler = new NativeMessageHandler(false,true); 
      HttpClient client = (handler == null) ? new HttpClient() :new HttpClient(new NativeMessageHandler(false,true)); 
       string json = JsonConvert.SerializeObject(content); 
       var address = string.Format("{0}{1}", WebService.BaseUrl, url); 
       var httpContent = new StringContent(json, Encoding.UTF8, "application/json"); 
       var response = client.PutAsync(new System.Uri(address), httpContent); 
       var stream = await response.Result.Content.ReadAsStringAsync(); 
      return stream; 

     } 
    } 
} 
1

でテストしています。私はそれが働いていた私が得た私のコ​​ードを掲載していますhttps://developer.xamarin.com/guides/xamarin-forms/web-services/authentication/oauth/

+0

私はOAuthを使用していません。私は次の投稿http://www.sylapse.com/xamarin/mobile-rest-client-with-xamarin/からの助けを得ようとしました 私はこの中で正確に言及されたサービスを呼び出そうとしましたが、私のサービスリクエストは通過しません。私が使用しているサンプルコードで私の更新された記事を見てください – Biju