代码之家  ›  专栏  ›  技术社区  ›  Brian

中的多租户授权。NET Core 2.0:如何

  •  0
  • Brian  · 技术社区  · 7 年前

    我想在中设置多租户。净核心。其基本思想是:

    • 将JWT用于API
    • 允许用户使用Azure AD、Google、Facebook等服务登录
    • 利用标识允许用户使用站点帐户登录

    任何使用第三方提供商(Azure AD、Google等)登录的人都需要首次“注册”。然后,他们的Google帐户将与用户帐户链接。JWT令牌还将映射到具有特定权限的用户帐户。

    我最初的尝试是这样做:

    services.AddAuthentication(sharedOptions =>
    {
       sharedOptions.DefaultScheme  =cookieAuthenticationDefaults.AuthenticationScheme;
                    sharedOptions.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
                })
                .AddGoogle(optoins =>  Configuration.Bind("Google", options)) 
                .AddAzureAd(options => Configuration.Bind("AzureAd", options))
                .AddCookie();
    

    问题是,现在我有了多个挑战和身份验证方案,所以我的计划是删除默认方案,并为用户提供一种选择他们想要使用的方案的方法。

    我所做的是子类 AuthenticationService 和覆盖 AuthenticateAsync , ChallengeAsync SignInAsync . Authenticate和Challenege将重新指向一个页面,用户将在该页面上选择其身份验证首选项。然后,他们的选择存储在cookie中,并用于后续的身份验证和查询调用。到目前为止一切正常,除了现在 符号同步 运行没有注册的处理程序。

    在这个阶段,我想知道我解决问题的方法是否正确:似乎初创公司应该注册我需要的处理程序。我确信有一种方法可以根据用户的选择注册处理程序,但我想知道这是否正确。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Brian    7 年前

    在阅读了Microsoft的identity文档后,似乎为了让他们的示例按书面形式工作,需要使用identity。呼叫 AddIdentity AddDefaultTokenProviders 配置默认值以下代码将或多或少按照我的要求执行(JWT除外)

     //configure identity
                services.AddDbContext<ApplicationDbContext>(options =>
                    options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
    
                services.AddIdentity<ApplicationUser, IdentityRole>()
                    .AddEntityFrameworkStores<ApplicationDbContext>()
                    .AddDefaultTokenProviders();
    
                //configure other providers
                var authSection = Configuration.GetSection("Authentication");
    
                 var authBuilder=services.AddAuthentication()
                .AddGoogle(options =>  authSection.Bind("GooglePlus", options))
                .AddAzureAd(options => authSection.Bind("AzureAd", options));
    

    医生说:

    注: 呼叫 额外性 配置默认方案设置。这个 AddAuthentication(string defaultScheme) 重载设置 DefaultScheme 所有物而且 AddAuthentication(Action<AuthenticationOptions> configureOptions) 重载仅设置显式设置的属性。添加多个身份验证提供程序时,这些重载中的任何一个都只应调用一次。对它的后续调用可能会覆盖任何以前配置的 AuthenticationOptions 属性。