Exemple #1
0
        public void Run(string environmentName)
        {
            // IdentityResource
            _context.RemoveRange(_context.IdentityResources);
            _context.SaveChanges();

            foreach (var item in IdentityResourceData.GetList(environmentName))
            {
                _context.IdentityResources.Add(item.ToEntity());
            }
            _context.SaveChanges();

            // ApiResource
            _context.RemoveRange(_context.ApiResources);
            _context.SaveChanges();

            foreach (var item in ApiResourceData.GetList(environmentName))
            {
                _context.ApiResources.Add(item.ToEntity());
            }
            _context.SaveChanges();

            // Client
            _context.RemoveRange(_context.Clients);
            _context.SaveChanges();

            foreach (var item in ClientData.GetList(environmentName))
            {
                _context.Clients.Add(item.ToEntity());
            }
            _context.SaveChanges();
        }
Exemple #2
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();
            }
        }
        private static async Task AddIdentityResourceIfExists(this IApplicationBuilder app, string name, IdentityResource identity)
        {
            var identityResourceDataAccess = app.ApplicationServices.GetService <IIdentityResourceDataAccess>();
            var data = await identityResourceDataAccess.GetByField(data => data.Name, name);

            if (data == null)
            {
                var newIdentityData = new IdentityResourceData(string.Empty, identity.Name, identity.DisplayName, identity.Description, identity.UserClaims, identity.Enabled);
                await identityResourceDataAccess.InsertAsync(newIdentityData);
            }
        }
        public static IdentityResourceModel ToModel(this IdentityResourceData data)
        {
            var model = new IdentityResourceModel()
            {
                Id          = data.Id,
                Name        = data.Name,
                DisplayName = data.DisplayName,
                Description = data.Description,
                Enabled     = data.Enabled,
                UserClaims  = data.UserClaims
            } ?? new IdentityResourceModel();

            return(model);
        }
 private bool CheckDataAndIdentityModel(IdentityResourceData data, IdentityResourceModel model)
 {
     return
         (data != null
          &&
          model != null
          &&
          data.Name == model.Name
          &&
          data.DisplayName == model.DisplayName
          &&
          data.Description == model.Description
          &&
          data.Enabled == model.Enabled
          &&
          data.Id == model.Id
          &&
          data.UserClaims.All(dataUserClaim => model.UserClaims.Contains(dataUserClaim)));
 }
 public async Task UpsertIdentityResource(IdentityResourceInputModel model)
 {
     try
     {
         var data = new IdentityResourceData(model.Id, model.Name, model.DisplayName, model.Description, model.UserClaims);
         if (string.IsNullOrWhiteSpace(model.Id))
         {
             await IdentityResourceDataAccess.InsertAsync(data);
         }
         else
         {
             await IdentityResourceDataAccess.ReplaceAsync(data, updateData => updateData.Id == data.Id);
         }
     }
     catch (MongoWriteException ex)
     {
         ex.ThrowIfDuplicateKey(nameof(model.Name), $"O nome '{model.Name}' já existe.");
         throw ex;
     }
 }