【问题标题】:Azure App Service Owin Instagram and other Auth providersAzure 应用服务 Owin Instagram 和其他身份验证提供程序
【发布时间】:2016-10-19 18:29:44
【问题描述】:

我刚刚升级了我的旧 Azure 移动服务应用程序,该应用程序具有除 facebook、google、twitter 等之外的各种其他身份验证方法。

我认为它属于 2.01。

他们看起来像这样:

因此实现了 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 应用服务中使用更新的东西来做到这一点的正确方法是什么?

我在这里添加了一些库和额外的东西,但我在任何地方都找不到任何文档(如果不知道文档在哪里会有帮助):

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);
        }
    }
}

感谢您的帮助:)

【问题讨论】:

    标签: .net azure oauth owin azure-app-service-envrmnt


    【解决方案1】:

    如果我对您的理解正确,我假设您提供了三种身份验证方法(Facebook、Instagram、Twitter)。而且您已经在旧的 Azure 移动服务应用程序中自己实现了 Instagram 的 LoginProvider。

    Authentication and authorization in Azure App Service,我们可以发现:

    应用服务支持五个开箱即用的身份提供程序:Azure Active Directory、Facebook、Google、Microsoft Account 和 Twitter。要扩展内置支持,您可以集成另一个身份提供者或your own custom identity solution

    对于使用IAppBuilder.UseAppServiceAuthentication,您可以尝试关注这个官方tutorial和这个示例azure-mobile-apps-net-server

    【讨论】:

    • 是的,您已经正确理解,我创建的应用程序从 twitter、facebook、instagram 和其他一些网站中提取信息。以前,用户可以从这些提要中登录(并存储令牌),以便用户可以使用他们的各种 api 与每个提要进行交互。我已按照您建议的教程实施自定义身份验证以自行登录应用程序,但无法像以前一样使用 owin.security.instgram 库让 instagram 正常工作:(
    猜你喜欢
    • 2018-10-31
    • 2017-04-23
    • 2018-12-20
    • 1970-01-01
    • 2020-04-23
    • 1970-01-01
    • 1970-01-01
    • 2015-08-29
    • 2017-05-28
    相关资源
    最近更新 更多