// This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure <AzureAdB2COptions>(Configuration.GetSection("Authentication:AzureAdB2C"));
            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();

            services.AddDistributedMemoryCache();
            services.AddSession(options =>
            {
                options.IdleTimeout    = TimeSpan.FromHours(1);
                options.CookieHttpOnly = true;
            });

            var sp = services.BuildServiceProvider();
            AzureAdB2COptions AzureAdB2COptions = new AzureAdB2COptions();

            Configuration.GetSection("Authentication:AzureAdB2C").Bind(AzureAdB2COptions);
            services.AddAuthentication(auth =>
            {
                auth.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                auth.DefaultChallengeScheme    = OpenIdConnectDefaults.AuthenticationScheme;
                auth.DefaultSignInScheme       = CookieAuthenticationDefaults.AuthenticationScheme;
            })
            .AddCookie()
            .AddOpenIdConnect(options =>
            {
                OpenIdConnectOptionsSetup OpenIdConnectOptionsSetup = new OpenIdConnectOptionsSetup(AzureAdB2COptions);
                options.ClientId                  = OpenIdConnectOptionsSetup.AzureAdB2COptions.ClientId;
                options.Authority                 = OpenIdConnectOptionsSetup.AzureAdB2COptions.Authority;
                options.UseTokenLifetime          = true;
                options.TokenValidationParameters = new TokenValidationParameters()
                {
                    NameClaimType = "name"
                };

                options.Events = new OpenIdConnectEvents()
                {
                    OnRedirectToIdentityProvider = OpenIdConnectOptionsSetup.OnRedirectToIdentityProvider,
                    OnRemoteFailure             = OpenIdConnectOptionsSetup.OnRemoteFailure,
                    OnAuthorizationCodeReceived = OpenIdConnectOptionsSetup.OnAuthorizationCodeReceived
                };
            });

            services.AddMvc();
        }
Esempio n. 2
0
 public OpenIdConnectOptionsSetup(IOptions <AzureAdB2COptions> b2cOptions)
 {
     AzureAdB2COptions = b2cOptions.Value;
 }
Esempio n. 3
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            azureAdB2COptions = new AzureAdB2COptions();
            Configuration.Bind("AzureAdB2C", azureAdB2COptions);

            azureAdOptions = new AzureAdOptions();
            Configuration.Bind("AzureAd", azureAdOptions);

            services.Configure <AzureAdB2COptions>(options => Configuration.GetSection("AzureAdB2C").Bind(options));
            services.Configure <AzureAdOptions>(options => Configuration.GetSection("AzureAd").Bind(options));
            services.AddAuthentication(sharedOptions =>
            {
                sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            })
            .AddOpenIdConnect(options =>
            {
                options.Authority                 = $"{Configuration["AzureAd:Instance"]}{Configuration["AzureAd:TenantId"]}/v2.0/";
                options.ClientId                  = Configuration["AzureAd:ClientId"];
                options.RequireHttpsMetadata      = false;
                options.CallbackPath              = Configuration["AzureAd:CallbackPath"];
                options.SignedOutCallbackPath     = Configuration["AzureAd:SignOutCallbackPath"];
                options.UseTokenLifetime          = true;
                options.ResponseType              = OpenIdConnectResponseType.Code + " " + OpenIdConnectResponseType.IdToken;
                options.ResponseMode              = "form_post";
                options.SaveTokens                = true;
                options.TokenValidationParameters = new TokenValidationParameters()
                {
                    NameClaimType = "name", ValidateIssuer = false
                };
                options.Events = new OpenIdConnectEvents()
                {
                    OnRemoteFailure             = OnRemoteFailureAsync,
                    OnAuthorizationCodeReceived = OnAuthorizationCodeReceivedAsync
                                                  //OnAuthenticationFailed = OnAuthenticationFailedAsync
                };
            })
            .AddOpenIdConnect("AzureAdB2C", options =>
            {
                options.Authority                 = $"{Configuration["AzureAdB2C:Instance"]}{Configuration["AzureAdB2C:TenantId"]}/{Configuration["AzureAdB2C:SignUpSignInPolicyId"]}/v2.0/";
                options.ClientId                  = Configuration["AzureAdB2C:ClientId"];
                options.RequireHttpsMetadata      = false;
                options.SignedOutCallbackPath     = Configuration["AzureAdB2C:SignOutCallbackPath"];
                options.CallbackPath              = Configuration["AzureAdB2C:CallbackPath"];
                options.TokenValidationParameters = new TokenValidationParameters()
                {
                    NameClaimType = "name", ValidateIssuer = false
                };
                options.Resource = "https://graph.windows.net";
                options.Events   = new OpenIdConnectEvents()
                {
                    OnRedirectToIdentityProvider = OnRedirectToIdentityProviderB2CAsync,
                    OnRemoteFailure             = OnRemoteFailureB2cAsync,
                    OnAuthorizationCodeReceived = OnAuthorizationCodeReceivedB2CAsync
                };
            })
            .AddCookie();


            services.AddMvc()
            .AddSessionStateTempDataProvider();
            services.AddSession();
        }
 public OpenIdConnectOptionsSetup(AzureAdB2COptions b2cOptions)
 {
     AzureAdB2COptions = b2cOptions;
 }