Пример #1
0
 public AccountController(
     UserManager <ApplicationUser> userManager,
     MySignInManager <ApplicationUser> signInManager,
     IEmailSender emailSender,
     ISmsSender smsSender,
     ILoggerFactory loggerFactory, TokenAuthManager tokenOptions)
 {
     _userManager   = userManager;
     _signInManager = signInManager;
     _emailSender   = emailSender;
     _smsSender     = smsSender;
     _logger        = loggerFactory.CreateLogger <AccountController>();
     _tokenManager  = tokenOptions;
 }
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
#if DEBUG
            services.AddEntityFramework()
            .AddSqlServer()
            .AddDbContext <AuthorizationDbContext>(options =>
                                                   options.UseSqlServer(Configuration["Data:DefaultConnection:AuthConnectionString"]))
            .AddDbContext <DataDbContext>(options =>
                                          options.UseSqlServer(Configuration["Data:DefaultConnection:DataConnectionString"]));
#elif RELEASE
            services.AddEntityFramework()
            .AddSqlServer()
            .AddDbContext <AuthorizationDbContext>(options =>
                                                   options.UseSqlServer(Configuration["Data:AzureConnection:AuthConnectionString"]))
            .AddDbContext <DataDbContext>(options =>
                                          options.UseSqlServer(Configuration["Data:AzureConnection:DataConnectionString"]));

            services.Configure <MvcOptions>(options =>
            {
                options.Filters.Add(new RequireHttpsAttribute());
            });
#endif

            services.AddIdentity <ApplicationUser, IdentityRole>(o =>
            {
                // configure identity options
                o.Password.RequireDigit            = false;
                o.Password.RequireLowercase        = false;
                o.Password.RequireUppercase        = false;
                o.Password.RequireNonLetterOrDigit = false;;
                o.Password.RequiredLength          = 6;
            })
            .AddEntityFrameworkStores <AuthorizationDbContext>()
            .AddDefaultTokenProviders();

            services.AddScoped <MySignInManager <ApplicationUser>, MySignInManager <ApplicationUser> >(); //rzekomo dzięki temu można zastąpić SignInManagera swoim własnym

            //Token-based authentication https://github.com/mrsheepuk/ASPNETSelfCreatedTokenAuthExample
            // *** CHANGE THIS FOR PRODUCTION USE ***
            // Here, we're generating a random key to sign tokens - obviously this means
            // that each time the app is started the key will change, and multiple servers
            // all have different keys. This should be changed to load a key from a file
            // securely delivered to your application, controlled by configuration.
            //
            // See the RSAKeyUtils.GetKeyParameters method for an examle of loading from
            // a JSON file.
            RSAParameters keyParams = RSAKeyUtils.GetRandomKey();

            // Create the key, and a set of token options to record signing credentials
            // using that key, along with the other parameters we will need in the
            // token controlller.
            key          = new RsaSecurityKey(keyParams);
            tokenOptions = new TokenAuthManager()
            {
                Audience           = TokenAudience,
                Issuer             = TokenIssuer,
                SigningCredentials = new SigningCredentials(key, SecurityAlgorithms.RsaSha256Signature)
            };

            // Save the token options into an instance so they're accessible to the
            // controller.
            services.AddInstance <TokenAuthManager>(tokenOptions);

            // Enable the use of an [Authorize("Bearer")] attribute on methods and classes to protect.
            services.AddAuthorization(auth =>
            {
                auth.AddPolicy("Bearer", new AuthorizationPolicyBuilder()
                               .AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme‌​)
                               .RequireAuthenticatedUser().RequireClaim(ClaimTypes.NameIdentifier).Build());
            });

            //Koniec Token-based authentication

            services.AddMvc();

            // Add application services.
            services.AddTransient <IEmailSender, AuthMessageSender>();
            services.AddTransient <ISmsSender, AuthMessageSender>();
        }
Пример #3
0
 public HomeController(DataDbContext context, TokenAuthManager tokenOptions)
 {
     db            = context;
     _tokenOptions = tokenOptions;
 }