Exemplo n.º 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)
        {
            CookieAuthName = "CookieAuth";
            _cookieName    = "MyCookie";

            var connectionString = _configuration.GetConnectionString(AuthorizationConnectionStringName);

            SMSoptions.Configure(_configuration["SmsOptions:SMSAccountIdentification"],
                                 _configuration["SmsOptions:SMSAccountPassword"],
                                 _configuration["SmsOptions:SMSAccountFrom"]);

            services
            .AddDbContext <UsersContest>(options =>
            {
                options.UseSqlServer(connectionString);
            })
            // Добавление Microsoft Identity
            .AddIdentity <User, Role>(config =>
            {
                config.Password.RequireNonAlphanumeric = false;
                config.Password.RequireUppercase       = false;
                config.Password.RequireLowercase       = false;
            })
            // Обязательно надо! Иначе не поймет какой контекст использовать
            .AddEntityFrameworkStores <UsersContest>()
            .AddDefaultTokenProviders();

            // Нужно, чтобы сконфигурировать куки
            services.ConfigureApplicationCookie(config =>
            {
                config.Cookie.Name      = _cookieName;
                config.LoginPath        = "/admin/login";
                config.AccessDeniedPath = "/admin/accessdenied";
            });

            // Здесь регулируем policy
            services.AddAuthorization(options =>
            {
                options.AddPolicy(Policies.AdministratorPolicy, builder =>
                {
                    builder.RequireClaim(ClaimTypes.Role, Policies.AdministratorPolicy);
                });

                options.AddPolicy(Policies.MangerPolicy, builder =>
                {
                    builder.RequireAssertion(y => y.User.HasClaim(ClaimTypes.Role, Policies.AdministratorPolicy) ||
                                             y.User.HasClaim(ClaimTypes.Role, Policies.MangerPolicy));
                });
            });

            services.AddControllersWithViews();

            services.AddSingleton <ISmsService, SmsService>();
        }
        }                                   // set only via Secret Manager

        public SmsSender(IOptions <SMSoptions> optionsAccessor)
        {
            Options = optionsAccessor.Value;
        }
Exemplo n.º 3
0
 }                                   // set only via Secret Manager
 public AuthMessageSender(IOptions <SMSoptions> optionsAccessor)
 {
     Options = optionsAccessor.Value;
 }