1

Azure Mobile Appsの認証サービスに接続しているXamarin.Formsアプリ(iOS、Android、UWP)があります。私が読んだところから、実装するのはかなり簡単でした。私はUWPプロジェクトでテストしています。アンドロイドも同じ結果を得ています。認証 - Xamarin.Forms、Azureモバイルアプリ

ログインすると、このサービスはすぐに必要なサービスに接続できなくなります。私は自分のコードで何か間違っているとは思わない。ここで何が起こっているのでしょうか?

WindowsのUWP:

public async Task<bool> Authenticate() 
{ 
    string message = string.Empty; 
    var success = false; 

    try 
    { 
     // Sign in with Facebook login using a server-managed flow. 
     if (user == null) 
     { 
      user = await TaskService.DefaultService.CurrentClient.LoginAsync(MobileServiceAuthenticationProvider.Facebook); 
      if (user != null) 
      { 
       success = true; 
       message = string.Format("You are now signed-in as {0}.", user.UserId); 
      } 
     } 

    } 
    catch (Exception ex) 
    { 
     message = string.Format("Authentication Failed: {0}", ex.Message); 
    } 

    // Display the success or failure message. 
    await new MessageDialog(message, "Sign-in result").ShowAsync(); 

    return success; 
} 

アンドロイド:

public async Task<bool> Authenticate() 
{ 
    var success = false; 
    var message = string.Empty; 
    try 
    { 
     // Sign in with Facebook login using a server-managed flow. 
     user = await TaskService.DefaultService.CurrentClient.LoginAsync(this, 
      MobileServiceAuthenticationProvider.Facebook); 
     if (user != null) 
     { 
      message = string.Format("you are now signed-in as {0}.", 
       user.UserId); 
      success = true; 
     } 
    } 
    catch (Exception ex) 
    { 
     message = ex.Message; 
    } 

    // Display the success or failure message. 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.SetMessage(message); 
    builder.SetTitle("Sign-in result"); 
    builder.Create().Show(); 

    return success; 
} 

サービスエラーに接続できません: Can't connect to service error in Windows UWP

アプリのためのFacebook上での設定: Facebook App Settings

は訪問:

編集:私は、クライアントのコードを移動しようとしました App.cにS:

private static MobileServiceClient _Client; 
public static MobileServiceClient Client 
{ 
    get 
    { 
     if(_Client == null) _Client = new MobileServiceClient(AppConstants.AzureMobileServiceURL); 
     return _Client; 
    } 
    set { _Client = value; } 
} 

そしてMainPage.xaml.csで新しいコール:

uesr = await Slated.App.Client.LoginAsync(MobileServiceAuthenticationProvider.Facebook); 

同じ結果!

別のノート://

EDIT 2:私のAzureモバイルサービスURLががhttps含まないバックエンドにログインするために、以下の コードを、/ログイン/ログインfacebookにリダイレクトするときに問題になりそうです、認証の残りの部分はOKに進みます。 FYI - ____

2016-07-29T18:38:43 PID[6684] Verbose  Received request: GET https://________.azurewebsites.net/login/facebook 
2016-07-29T18:38:43 PID[6684] Information Redirecting: https://www.facebook.com/dialog/oauth?response_type=code&client_id=_____________&redirect_uri=https%3A%2F%2F________.azurewebsites.net%2F.auth%2Flogin%2Ffacebook%2Fcallback&scope=public_profile&state=_____________________________&display=popup 
2016-07-29T18:38:50 PID[6684] Verbose  Received request: GET https://________.azurewebsites.net/.auth/login/facebook/callback?code=____________________ 
2016-07-29T18:38:50 PID[6684] Verbose  Calling into external HTTP endpoint GET https://graph.facebook.com/oauth/access_token. 
2016-07-29T18:38:51 PID[6684] Verbose  Calling into external HTTP endpoint GET https://graph.facebook.com/oauth/access_token. 
2016-07-29T18:38:51 PID[6684] Verbose  Calling into external HTTP endpoint GET https://graph.facebook.com/me. 
2016-07-29T18:38:51 PID[6684] Information Login completed for 'Thomas Gardner'. Provider: 'facebook'. 
2016-07-29T18:38:51 PID[6684] Verbose  Writing 'AppServiceAuthSession' cookie for site '________.azurewebsites.net'. Length: 512. 
2016-07-29T18:38:51 PID[6684] Information Redirecting: https://________.azurewebsites.net/login/facebook 
2016-07-29T18:38:51 PID[6684] Verbose  Received request: GET https://________.azurewebsites.net/login/facebook 
2016-07-29T18:38:51 PID[6684] Verbose  Found 'AppServiceAuthSession' cookie for site '________.azurewebsites.net'. Length: 512. 
2016-07-29T18:38:51 PID[6684] Verbose  Authenticated Thomas Gardner successfully using 'Session Cookie' authentication. 
2016-07-29T18:38:52 PID[6684] Verbose  Received request: GET https://________.azurewebsites.net/login/facebook 
2016-07-29T18:38:52 PID[6684] Verbose  Found 'AppServiceAuthSession' cookie for site '________.azurewebsites.net'. Length: 512. 
2016-07-29T18:38:52 PID[6684] Verbose  Authenticated __________ successfully using 'Session Cookie' authentication. 
2016-07-29T18:38:52 PID[6684] Information Request, Method=GET, Url=https://________.azurewebsites.net/login/facebook, Message='https://________.azurewebsites.net/login/facebook' 
2016-07-29T18:38:52 PID[6684] Information Message='Will use same 'JsonMediaTypeFormatter' formatter', Operation=JsonMediaTypeFormatter.GetPerRequestFormatterInstance 
2016-07-29T18:38:52 PID[6684] Information Message='Selected formatter='JsonMediaTypeFormatter', content-type='application/json; charset=utf-8'', Operation=DefaultContentNegotiator.Negotiate 
2016-07-29T18:38:52 PID[6684] Information Response, Status=404 (NotFound), Method=GET, Url=https://________.azurewebsites.net/login/facebook, Message='Content-type='application/json; charset=utf-8', content-length=unknown' 

バックエンドのコードで機密データを置き換える:それは、MSFTのテンプレートコードからコピーされます。スタートアップにはほとんどされていません。 Startup.Mobile.App.cs

public partial class Startup 
{ 
    public static void ConfigureMobileApp(IAppBuilder app) 
    { 
     HttpConfiguration config = new HttpConfiguration(); 

     //For more information on Web API tracing, see http://go.microsoft.com/fwlink/?LinkId=620686 
     config.EnableSystemDiagnosticsTracing(); 

     new MobileAppConfiguration() 
      .UseDefaultConfiguration() 
      .ApplyTo(config); 

     // Use Entity Framework Code First to create database tables based on your DbContext 
     Database.SetInitializer(new SlatedInitializer()); 

     // To prevent Entity Framework from modifying your database schema, use a null database initializer 
     // Database.SetInitializer<SlatedContext>(null); 

     MobileAppSettingsDictionary settings = config.GetMobileAppSettingsProvider().GetMobileAppSettings(); 

     if (string.IsNullOrEmpty(settings.HostName)) 
     { 
      // This middleware is intended to be used locally for debugging. By default, HostName will 
      // only have a value when running in an App Service application. 
      app.UseAppServiceAuthentication(new AppServiceAuthenticationOptions 
      { 
       SigningKey = ConfigurationManager.AppSettings["SigningKey"], 
       ValidAudiences = new[] { ConfigurationManager.AppSettings["ValidAudience"] }, 
       ValidIssuers = new[] { ConfigurationManager.AppSettings["ValidIssuer"] }, 
       TokenHandler = config.GetAppServiceTokenHandler() 
      }); 
     } 
     app.UseWebApi(config); 
    } 
} 

public class SlatedInitializer : CreateDatabaseIfNotExists<SlatedContext> 
{ 
    protected override void Seed(SlatedContext context) 
    { 
     /*List<Tasks> todoItems = new List<Tasks> 
     { 
      new Tasks { Id = Guid.NewGuid().ToString(), Text = "First item", Complete = false }, 
      new Tasks { Id = Guid.NewGuid().ToString(), Text = "Second item", Complete = false }, 
     }; 

     foreach (Tasks todoItem in todoItems) 
     { 
      context.Set<Tasks>().Add(todoItem); 
     }*/ 

     base.Seed(context); 
    } 
} 
+0

バックエンドの診断ログを有効にしてログを確認して、エラーがないかどうか確認できますか。また、Authentication/Authorizationセクションの設定方法の詳細を提供できますか? –

+0

質問に追加されました。ご協力ありがとうございました。 – Thomas

+0

使用しているクライアントおよびサーバーパッケージのバージョンを含めることはできますか? – brettsam

答えて

1

使用されているのURIから、それが見えます。 2つはではなく、と互換性があります。

詳しくは、Client and server versioning in Mobile Apps and Mobile Servicesを参照してください。

サーバーでは、Microsoft.Azure.Mobile.Server。*を使用する必要があります。 WindowsAzure.MobileServices.Backendの形式のパッケージがないことを確認してください。

クライアントでは、パッケージMicrosoft.Azure.Mobile.Clientを使用する必要があります。

+0

完了しました。元々のように条件付きを含めるように更新しましたが、まだエラーが発生しており、バックエンドの診断ログも同じです。 – Thomas

+0

@Thomas更新されたコードであなたの投稿を更新できますか?すでに行った手順のトラブルシューティングは簡単ですか? –

+0

完了。ご協力ありがとうございました。 – Thomas

0
user = await App.MobileService .LoginAsync(MobileServiceAuthenticationProvider.Facebook); 

は、このコード行を試してみてください。これは動作するはずですし、ユーザID のためにあなたは、単にそのようにそれを行うことができます:あなたがモバイルサービスとモバイルアプリの間でパッケージを混合しているよう

var userId = user.Id; 
+0

if(user!= null)からif(user.UserId!= null)に変更することを前提とすると、それは役に立ちません。同じ結果。 – Thomas

+0

いいえ、 私はその行を置き換えることを意味します: user = await TaskService.DefaultService.CurrentClient.LoginAsync(MobileServiceAuthenticationProvider.Facebook); その行は次のとおりです。 user = await App.MobileService .LoginAsync(MobileServiceAuthenticationProvider.Facebook); –

+0

とし、app.xaml.cs のモバイルサービスのURLがhttpsで始まることを確認してください。それは必須です。 –

0

さてここで、私はそれを行う方法:

user = await App.MobileService.LoginAsync(MobileServiceAuthenticationProvider.Facebook); 

、それは私のために魔法のように動作し、そして確認してください。app.xaml.cs

public static MobileServiceClient MobileService = new MobileServiceClient("https://yourmobileservices.azurewebsites.net"); 
あなたの背後にあるコードで、その後

にトークンストアがオンになっている詳細設定の下にある 認証タブのazureポータルで

+0

これもやってください。それはすべてのガイドが約束しているものです。Facebookの認証設定に問題がありますか?リダイレクトURLにより、サーバーは404(.../login/facebook) – Thomas

+0

@ThomasがこのUWPを参照して応答します。[サンプル](https://code.msdn.microsoft.com/Facebook-authentification-bceea645)それはあなたを助けるでしょう。 –

関連する問題