如何通过编程将B2CLogin.com设置为默认的Azure B2C SSO URL?

时间:2018-08-24 19:52:46

标签: azure-ad-b2c

访问Azure B2C SSO时,我想使用B2Clogin.com而不是login.microsoftonline.com作为默认URL。

我已经读过https://docs.microsoft.com/en-us/azure/active-directory-b2c/b2clogin,它说我们可以将b2clogin.com与RunNow一起使用。我想知道如何以编程方式进行设置吗?

2 个答案:

答案 0 :(得分:0)

在应用程序配置中,您只需要将引用从https://login.microsoftonline.com更改为https://{tenant}.b2clogin.com

答案 1 :(得分:0)

嗨,

这是我的代码,它可与b2clogin.com一起使用:

这很重要:

如果您的租户名称:xxxxxxxx.onmicrosoft.com,则租户为xxxxxxxx (它指的是我的变量:)

namespace wp_front
{
    public class Startup
    {
        public IConfiguration Configuration { get; }
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddAuthentication(options =>
                {
                    options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                    options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                    options.DefaultChallengeScheme = Configuration["signUpInPolicy"];
                })
                .AddOpenIdConnect(Configuration.GetSection("Azure").GetSection("signUpPolicy").Value, GetOpenIdConnectOptions(Configuration.GetSection("Azure").GetSection("signUpPolicy").Value))
                .AddOpenIdConnect(Configuration.GetSection("Azure").GetSection("signInPolicy").Value, GetOpenIdConnectOptions(Configuration.GetSection("Azure").GetSection("signInPolicy").Value))
                .AddOpenIdConnect(Configuration.GetSection("Azure").GetSection("signUpInPolicy").Value, GetOpenIdConnectOptions(Configuration.GetSection("Azure").GetSection("signUpInPolicy").Value))
                .AddOpenIdConnect(Configuration.GetSection("Azure").GetSection("editProfilePolicy").Value, GetOpenIdConnectOptions(Configuration.GetSection("Azure").GetSection("editProfilePolicy").Value))

                .AddCookie();

            services.AddMvc();
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            app.UseStaticFiles();

            app.UseDeveloperExceptionPage();

            app.UseAuthentication();

            app.UseMvcWithDefaultRoute();
        }

        private Action<OpenIdConnectOptions> GetOpenIdConnectOptions(string policy)
            => options =>
            {
                options.MetadataAddress =
                    $"https://{Configuration.GetSection("Azure").GetSection("Tenant").Value}.b2clogin.com/{Configuration.GetSection("Azure").GetSection("TenantName").Value}/v2.0/.well-known/openid-configuration?p={policy}";
                options.ClientId = Configuration.GetSection("Azure").GetSection("ClientId").Value;
                options.ResponseType = OpenIdConnectResponseType.IdToken;
                options.CallbackPath = "/signin/" + policy;
                options.SignedOutCallbackPath = "/signout/" + policy;
                options.SignedOutRedirectUri = "/";
                options.TokenValidationParameters.NameClaimType = "name";
                options.Events.OnMessageReceived = context =>
                {
                    if (!string.IsNullOrEmpty(context.ProtocolMessage.Error) &&
                        !string.IsNullOrEmpty(context.ProtocolMessage.ErrorDescription) &&
                        context.ProtocolMessage.ErrorDescription.StartsWith("AADB2C90091"))
                    {
                        context.Response.Redirect("/");
                        context.HandleResponse();
                    }

                    return Task.FromResult(0);
                };
            };
    }
}