Exemplo n.º 1
0
 public AuthController(UserManager <UserAccount> userManager,
                       SignInManager <UserAccount> signInManager, RoleManager <Role> _roleManager,
                       IEmailSender emailSender, IHTMLGenerator _htmlGenerator, IClaimServiceFactory claimsManager,
                       IStringLocalizer <LocalizedIdentityErrorDescriber> localizer, ReservedRegisterNotifyPublisher _publisher,
                       AccountDataContext accountContext)
 {
     _userManager    = userManager;
     _accountContext = accountContext;
     _signInManager  = signInManager;
     _localizer      = localizer;
     _emailSender    = emailSender;
     htmlGenerator   = _htmlGenerator;
     Publisher       = _publisher;
     roleManager     = _roleManager;
     _claimsManager  = claimsManager;
 }
Exemplo n.º 2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            //MongoDB.Driver.MongoClient client = new MongoDB.Driver.MongoClient(Configuration.GetConnectionString("mongodb"));

            //services.AddDbContext<AccountRecordContext>(
            //    options => options.UseMySql(Configuration.GetConnectionString("mysqldb"))
            //    );

            services.AddDbContext <AccountDataContext>(
                options => options.UseMySql(Configuration.GetConnectionString("peiu_account_connnectionstring")));
            var EmailSettings = Configuration.GetSection("EmailSettings:SenderName");

            services.Configure <EmailSettings>(Configuration.GetSection("EmailSettings"));
            services.AddSingleton <IEmailSender, EmailSender>();
            services.AddSingleton <IHTMLGenerator, HTMLGenerator>();
            //services.AddIdentity<UserAccount>()
            services.AddIdentity <UserAccount, Role>(options =>
                                                     options.ClaimsIdentity.UserIdClaimType = "Id")
            .AddEntityFrameworkStores <AccountDataContext>()
            .AddErrorDescriber <Localization.LocalizedIdentityErrorDescriber>()
            .AddDefaultTokenProviders();

            //add the following line of code
            services.AddScoped <IUserClaimsPrincipalFactory <UserAccount>, ClaimsPrincipalFactory>();
            //ServiceDescriptor sd = services.FirstOrDefault(x => x.ServiceType == typeof(IdentityErrorDescriber) && x.ImplementationType == typeof(Localization.LocalizedIdentityErrorDescriber));
            //sd.
            services.AddAuthentication(x =>
            {
                x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                x.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            }).AddJwtBearer(options =>
            {
                options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
                {
                    ValidateIssuer           = false,
                    ValidateAudience         = false,
                    ValidateLifetime         = true,
                    ValidateIssuerSigningKey = true,
                    ValidIssuer      = UserClaimTypes.Issuer,
                    ValidAudience    = "https://www.peiu.co.kr",
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(JasonWebTokenManager.Secret))
                };
                options.ClaimsIssuer = UserClaimTypes.Issuer;
            });
            //options.Configuration.
            //.AddCookie();

            services.AddPortableObjectLocalization(options => options.ResourcesPath = "Localization");
            services.AddSingleton <PeiuGridDataContext>();


            ConfigureIdentity(services);
            ConfigureAuthrozation(services);
            services.AddCors();

            var map_reduces = Configuration.GetSection("MongoMapReduces").Get <IEnumerable <MongoMapReduceConfig> >();

            var redisConfiguration = Configuration.GetSection("redis").Get <RedisConfiguration>();

            services.AddSingleton(redisConfiguration);
            services.AddSingleton <IRedisConnectionFactory, RedisConnectionFactory>();
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info {
                    Title = "My API", Version = "v1"
                });
            });

            MqttAddress data_mqtt_address = Configuration.GetSection("MQTTBrokers:DataBrokerAddress").Get <MqttAddress>();

            services.AddSingleton(data_mqtt_address);
            services.AddHostedService <MqttSubscribeWorker>();

            ReservedRegisterNotifyPublisher reservedRegisterNotifyPublisher = new ReservedRegisterNotifyPublisher();

            reservedRegisterNotifyPublisher.Initialize();
            services.AddSingleton(reservedRegisterNotifyPublisher);
            services.AddSingleton <Microsoft.Extensions.Hosting.IHostedService, CollectingCurrentWeatherService>();
            //services.AddSingleton(client);


            //IServiceCollection cols  = services.AddSingleton<IBackgroundMongoTaskQueue, MongoBackgroundTaskQueue>();
            //services.AddSingleton<MQTTDaegunSubscribe>();


            //services.AddCors(options =>
            //{
            //    options.AddDefaultPolicy(
            //        builder =>
            //        {
            //            builder.WithOrigins("http://118.216.255.118:3011")
            //            .AllowAnyHeader()
            //                            .AllowAnyMethod();
            //        });
            //    options.AddPolicy("PeiuPolicy",
            //    builder =>
            //    {
            //        builder.AllowAnyOrigin()
            //        .AllowAnyMethod()
            //        .AllowAnyHeader();
            //        //.AllowCredentials();
            //    });
            //});
            //services.Configure<MvcOptions>(options =>
            //{
            //    options.Filters.Add(new RequireHttpsAttribute());
            //});
            //services.AddTransient<IEmailSender, EmailSender>();
            services.AddPortableObjectLocalization();
            services.AddSingleton <IClaimServiceFactory, ClaimServiceFactory>();
            services.AddMvc()
            .AddViewLocalization(Microsoft.AspNetCore.Mvc.Razor.LanguageViewLocationExpanderFormat.Suffix)
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }