2016-10-18 8 views
0

アイブ氏はちょうどFacebookの、グーグル以外の様々な他の認証方法を持っていた私の昔のAzureモバイルサービスのアプリをアップグレードなどさえずり...AzureのアプリケーションサービスOwin Instagramのや他の認証プロバイダ

私は2.01 owinそれを考えます。

彼らはこのように見えた

ので、実装Microsoft.WindowsAzure.Mobile.Service.Security.LoginProvider:

public static class WebApiConfig 
{ 
    public static void Register() 
    { 
     // Use this class to set configuration options for your mobile service 
     ConfigOptions options = new ConfigOptions(); 
     options.PushAuthorization = AuthorizationLevel.User; 
     options.LoginProviders.Add(typeof(FacebookLoginProvider)); 
     options.LoginProviders.Add(typeof(InstaLoginProvider)); 
     options.LoginProviders.Add(typeof(TwitterLoginProvider)); 

     // Use this class to set WebAPI configuration options 
     HttpConfiguration config = ServiceConfig.Initialize(new ConfigBuilder(options)); 

     // Set default and null value handling to "Include" for Json Serializer 
     config.Formatters.JsonFormatter.SerializerSettings.DefaultValueHandling = DefaultValueHandling.Include; 
     config.Formatters.JsonFormatter.SerializerSettings.NullValueHandling = NullValueHandling.Include; 

     Database.SetInitializer(new app_name_mobile_appInitializer()); 
    } 

}

プロバイダの実装:

using System; 
using System.Security.Claims; 
using System.Threading.Tasks; 
using app_name_mobile_appService.DataObjects; 
using app_name_mobile_appService.Models; 
using Microsoft.WindowsAzure.Mobile.Service; 
using Microsoft.WindowsAzure.Mobile.Service.Security; 
using Newtonsoft.Json.Linq; 
using Owin; 
using Owin.Security.Providers.Instagram; 
using System.Linq; 

namespace app_name_mobile_appService.Auth.ExtraLogins.Instagram 
{ 
    public class InstaLoginProvider : LoginProvider 
    { 
     internal const string ProviderName = "Instagram"; 

     public InstaLoginProvider(IServiceTokenHandler tokenHandler) 
      : base(tokenHandler) 
     { 
     } 

     public override string Name 
     { 
      get { return ProviderName; } 
     } 

     public override void ConfigureMiddleware(IAppBuilder appBuilder, 
      ServiceSettingsDictionary settings) 
     { 
      InstagramAuthenticationOptions options = new InstagramAuthenticationOptions() 
      { 
       ClientId = settings["InstagramClientId"], 
       ClientSecret = settings["InstagramClientSecret"], 
       AuthenticationType = this.Name, 
       Provider = new InstaLoginAuthenticationProvider() 
       { 
        OnAuthenticated = (context) => 
        { 
         ben_coomber_mobile_appContext mainContext = new app_name_mobile_appContext(); 

         Account account = mainContext.Accounts.SingleOrDefault(a => a.UserIdWithProvider == context.Id); 

         if (account == null) 
         { 
          Account newAccount = new Account 
          { 
           Id = Guid.NewGuid().ToString(), 
           Username = context.UserName, 
           InstagramToken = context.AccessToken, 
           UserIdWithProvider = context.Id, 
           ProviderType = "instagram" 
          }; 
          mainContext.Accounts.Add(newAccount); 
          mainContext.SaveChanges(); 
         } 
         return Task.FromResult(0); 
        } 
       } 
      }; 
      options.Scope.Add("likes"); 
      options.Scope.Add("comments"); 
      appBuilder.UseInstagramInAuthentication(options); 
     } 

     public override ProviderCredentials CreateCredentials(
      ClaimsIdentity claimsIdentity) 
     { 
      Claim name = claimsIdentity.FindFirst(ClaimTypes.NameIdentifier); 
      Claim providerAccessToken = claimsIdentity 
       .FindFirst(ServiceClaimTypes.ProviderAccessToken); 

      InstaCredentials credentials = new InstaCredentials() 
      { 
       UserId = this.TokenHandler.CreateUserId(this.Name, name != null ? 
        name.Value : null), 
       AccessToken = providerAccessToken != null ? 
        providerAccessToken.Value : null 
      }; 

      return credentials; 
     } 

     public override ProviderCredentials ParseCredentials(JObject serialized) 
     { 
      return serialized.ToObject<InstaCredentials>(); 
     } 
    } 
} 

InstagramAuthenticationProvider実装:

using System.Security.Claims; 
using System.Threading.Tasks; 
using Microsoft.WindowsAzure.Mobile.Service.Security; 
using Owin.Security.Providers.Instagram.Provider; 

namespace app_name_mobile_appService.Auth.ExtraLogins.Instagram 
{ 
    public class InstaLoginAuthenticationProvider :InstagramAuthenticationProvider 
    { 
     public override Task Authenticated(InstagramAuthenticatedContext context) 
     { 
      context.Identity.AddClaim(
       new Claim(ServiceClaimTypes.ProviderAccessToken, context.AccessToken)); 
      return base.Authenticated(context); 
     } 
    } 
} 

ProviderCredentials実装:

using Microsoft.WindowsAzure.Mobile.Service.Security; 

namespace app_name_mobile_appService.Auth.ExtraLogins.Instagram 
{ 
    public class InstaCredentials : ProviderCredentials 
    { 
     public InstaCredentials() 
      : base(InstaLoginProvider.ProviderName) 
     { 
     } 

     public string AccessToken { get; set; } 
    } 
} 

Azureのアプリケーションサービスで、新しいものを使用して、これを行うための正しい方法をいただきましたので?

イムそのいくつかのlibs、余分なものがここに追加けど(が参考になるだけでドキュメントがどこにあるか知っているOT)ここで、iは、任意の任意のドキュメントを見つけるカントashumming:

using System.Web.Http; 
using Microsoft.Azure.Mobile.Server.Authentication; 
using Microsoft.Azure.Mobile.Server.Config; 
using Microsoft.Owin; 
using Owin; 

[assembly: OwinStartup(typeof(ben_coomber_mobile_appService.OwinStartUp))] 

namespace app_name_mobile_appService 
{ 
    public class OwinStartUp 
    { 
     public void Configuration(IAppBuilder app) 
     { 
      // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888 

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

      app.UseWebApi(config); 

      AppServiceAuthenticationOptions options = new AppServiceAuthenticationOptions(); 

      app.UseAppServiceAuthentication(options); 
     } 
    } 
} 

感謝任意のヘルプをありがとう:)

答えて

0

私はあなたを正しく理解していれば、認証のために3つの方法(Facebook、Instagram、Twitter)を提供することを前提としています。また、古いAzureモバイルサービスでLoginProvider for Instagramを自分で実装しました。

Authentication and authorization in Azure App Serviceから、我々はそれを見つけることができる:AzureのActive Directoryの、Facebookの、グーグル、マイクロソフトアカウント、およびツイッター:

アプリケーションサービスは、ボックスのうち5つのIDプロバイダーをサポートしています。組み込みサポートを拡張するには、別のIDプロバイダまたはyour own custom identity solutionを統合することができます。 IAppBuilder.UseAppServiceAuthenticationを使用するための

、あなたはこの公式tutorialと、このサンプルazure-mobile-apps-net-serverに従うことを試みることができます。

+0

はい、正しく理解しています。アプリケーションは、Twitter、Facebook、Instagramなどのフィードからプルを作成しています。これらのフィードから、ユーザーはさまざまなAPIを使用して各フィードとやりとりできるように、以前にサインイン(およびトークンを格納)することができます。私はあなたのアプリケーションに署名するためのカスタム認証を実装するために提案したチュートリアルに従ってきましたが、以前のようにowin.security.instgramライブラリを使用してinstagramを動作させることはできません: – user2120315

関連する問題