コード例 #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext <ApplicationDbContext>(options =>
            {
                options.UseSqlServer(_configuration["ConnectionString"]);
            });

            services.AddIdentity <ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores <ApplicationDbContext>()
            .AddDefaultTokenProviders();

            if (_environment.EnvironmentName != "Offline")
            {
                services.AddDataProtectionWithSqlServerForIdentityService(_configuration);
            }

            services.AddHsts(opts =>
            {
                opts.IncludeSubDomains = true;
                opts.MaxAge            = TimeSpan.FromSeconds(15768000);
            });

            services.AddControllersWithViews();

            var builder = services.AddIdentityServer(options =>
            {
                options.Events.RaiseErrorEvents       = true;
                options.Events.RaiseFailureEvents     = true;
                options.Events.RaiseInformationEvents = true;
                options.Events.RaiseSuccessEvents     = true;
            })
                          .AddInMemoryIdentityResources(IdentityResourceData.Resources())
                          .AddInMemoryApiResources(ApiResourceData.Resources())
                          .AddInMemoryApiScopes(ApiScopeData.Resources())
                          .AddInMemoryClients(ClientData.GetClients())
                          .AddOperationalStore(options =>
            {
                options.EnableTokenCleanup = true;
                //The number of records to remove at a time. Defaults to 100.
                options.TokenCleanupBatchSize = 100;
                options.TokenCleanupInterval  = 3600;            //Seconds

                options.ConfigureDbContext = b =>
                {
                    options.ConfigureDbContext = c =>
                                                 c.UseSqlServer(_configuration["ConnectionString"]);
                };
            })
                          .AddAspNetIdentity <ApplicationUser>();

            if (_environment.EnvironmentName != "Offline")
            {
                builder.AddProductionSigningCredential(_configuration);
            }
            else
            {
                builder.AddDeveloperSigningCredential();
            }
        }
コード例 #2
0
 private bool CheckApiScopeAndApiScopeData(ApiScopeData data, IdentityApiScope apiScope)
 {
     return
         (data != null
          &&
          apiScope != null
          &&
          data.Name == apiScope.Name
          &&
          data.DisplayName == apiScope.DisplayName
          &&
          data.Description == apiScope.Description
          &&
          data.Enabled == apiScope.Enabled);
 }
コード例 #3
0
 public async Task UpsertApiScopeAsync(ApiScopeInputModel apiScopeInputModel)
 {
     try
     {
         var apiScopeData = new ApiScopeData(apiScopeInputModel.Id, apiScopeInputModel.Name, apiScopeInputModel.DisplayName, apiScopeInputModel.Description);
         if (string.IsNullOrEmpty(apiScopeData.Id))
         {
             await ApiScopeDataAccess.InsertAsync(apiScopeData);
         }
         else
         {
             await ApiScopeDataAccess.ReplaceAsync(apiScopeData, data => data.Id == apiScopeData.Id);
         }
     }
     catch (MongoWriteException ex)
     {
         ex.ThrowIfDuplicateKey(nameof(apiScopeInputModel.Name), $"O nome '{apiScopeInputModel.Name} já existe.");
         throw ex;
     }
 }
コード例 #4
0
        public void GivenItHasApiScope_WhenitCallsEnabled_AndApiScopeIsDesabled_ThenShouldEnable()
        {
            // Given
            var id           = "1";
            var apiScopeData = new ApiScopeData(id, name: "ApiScope", displayName: "Api Scope", description: "Api Scope Description", enabled: true);

            ApiScopeDataAccessMock
            .Setup(dataAccess => dataAccess.GetByField(It.IsAny <Expression <Func <ApiScopeData, string> > >(), It.IsAny <string>()))
            .ReturnsAsync(apiScopeData);

            ApiScopeDataAccessMock
            .Setup(dataAccess => dataAccess.UpdateAsync(It.IsAny <Expression <Func <ApiScopeData, string> > >(), It.IsAny <string>(), It.IsAny <UpdateDefinition <ApiScopeData> >()))
            .ReturnsAsync(true);

            // When
            var apiScope = ApiScopeService.EnableApiScopeAsync(id).GetAwaiter().GetResult();

            // Then
            ApiScopeDataAccessMock.Verify(dataAccess => dataAccess.GetByField(It.IsAny <Expression <Func <ApiScopeData, string> > >(), It.IsAny <string>()), Times.Once);
            ApiScopeDataAccessMock.Verify(dataAccess => dataAccess.UpdateAsync(It.IsAny <Expression <Func <ApiScopeData, string> > >(), It.IsAny <string>(), It.IsAny <UpdateDefinition <ApiScopeData> >()), Times.Once);
            Assert.IsTrue(CheckApiScopeAndApiScopeData(apiScopeData, apiScope));
        }
コード例 #5
0
 private ApiScopeModel FromApiScopeData(ApiScopeData data)
 {
     return(new ApiScopeModel(data.Id, data.Name, data.DisplayName, data.Description, data.Enabled));
 }