Beispiel #1
0
        /// <summary>加载时触发</summary>
        protected override void OnLoaded()
        {
            if (TokenSecret.IsNullOrEmpty() || TokenSecret.Split(':').Length != 2)
            {
                TokenSecret = $"HS256:{Rand.NextString(16)}";
            }

            base.OnLoaded();
        }
 public IdentityService(UserManager <AppUser> userManager,
                        IUserService userService,
                        IAdminService adminService,
                        RoleManager <IdentityRole> roleManager,
                        TokenSecret tokenSecret,
                        IEmailSenderService emailSender,
                        IEmailBuilder emailBuilder,
                        ITokenValidator tokenValidator)
 {
     _userManager    = userManager;
     _userService    = userService;
     _adminService   = adminService;
     _roleManager    = roleManager;
     _tokenSecret    = tokenSecret;
     _emailSender    = emailSender;
     _emailBuilder   = emailBuilder;
     _tokenValidator = tokenValidator;
 }
Beispiel #3
0
 public UserService(IOptions <TokenSecret> appSettings)
 {
     this.secret = appSettings.Value;
 }
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDomainServices(Configuration);
            //In-Memory
            services.AddDistributedMemoryCache(); // Adds a default in-memory implementation of IDistributedCache
            services.AddSession();
            var tokenSecret = new TokenSecret();

            Configuration.Bind(nameof(tokenSecret), tokenSecret);
            services.AddSingleton(tokenSecret);

            services.AddAuthentication(auth =>
            {
                auth.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                auth.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
                auth.DefaultScheme             = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(options =>
            {
                options.SaveToken = true;
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(tokenSecret.SecretString)),
                    ValidateIssuer           = false,
                    ValidateAudience         = false,
                    RequireExpirationTime    = false,
                    ValidateLifetime         = true
                };
            })
            //.AddGoogle(options =>
            //{
            //    IConfigurationSection googleAuthNSection =
            //        Configuration.GetSection("Authentication:Google");

            //    options.ClientId = googleAuthNSection["ClientId"];
            //    options.ClientSecret = googleAuthNSection["ClientSecret"];
            //})
            //.AddTwitter(twitterOptions => {  })
            //.AddFacebook(facebookOptions => {  })
            ;

            services.AddOpenApiDocument(config =>
            {
                config.PostProcess = document =>
                {
                    document.Info.Title       = ApiDocConstants.TITLE;
                    document.Info.Description = ApiDocConstants.DESCRIPTION;
                    document.Info.Version     = ApiDocConstants.VERSION;
                    document.Info.Contact     = new NSwag.OpenApiContact
                    {
                        Name  = ApiDocConstants.CONTACT_NAME,
                        Email = ApiDocConstants.CONTACT_EMAIL,
                        Url   = ApiDocConstants.CONTACT_URL
                    };
                };
                config.DocumentProcessors.Add(
                    new SecurityDefinitionAppender("Bearer",
                                                   new OpenApiSecurityScheme
                {
                    Description = "JWT Authorization header using the bearer scheme",
                    Name        = "Authorization",
                    In          = OpenApiSecurityApiKeyLocation.Header,
                    Type        = OpenApiSecuritySchemeType.ApiKey
                }));
                config.OperationProcessors.Add(new AspNetCoreOperationSecurityScopeProcessor("Bearer"));
            });

            services.AddControllers()
            .AddNewtonsoftJson()
            .AddFluentValidation(fluentValidation =>
            {
                fluentValidation.RunDefaultMvcValidationAfterFluentValidationExecutes = false;
                fluentValidation.RegisterValidatorsFromAssemblyContaining <DishToBasketAddValidation>();
                fluentValidation.RegisterValidatorsFromAssemblyContaining <DishByPriceValidation>();
                fluentValidation.RegisterValidatorsFromAssemblyContaining <DishToAddValidation>();
                fluentValidation.RegisterValidatorsFromAssemblyContaining <DishToUpdateValidation>();
                fluentValidation.RegisterValidatorsFromAssemblyContaining <TagToAddValidation>();
                fluentValidation.RegisterValidatorsFromAssemblyContaining <TagToUpdateValidation>();
                fluentValidation.RegisterValidatorsFromAssemblyContaining <ReviewToAddValidation>();
                fluentValidation.RegisterValidatorsFromAssemblyContaining <ReviewToUpdateValidation>();
                fluentValidation.RegisterValidatorsFromAssemblyContaining <OrderToAddValidation>();
                fluentValidation.RegisterValidatorsFromAssemblyContaining <OrderToStatusUpdateValidation>();
                fluentValidation.RegisterValidatorsFromAssemblyContaining <OrderToUpdateValidation>();
                fluentValidation.RegisterValidatorsFromAssemblyContaining <UserPasswordToChangeValidation>();
                fluentValidation.RegisterValidatorsFromAssemblyContaining <UserToUpdateValidation>();
                fluentValidation.RegisterValidatorsFromAssemblyContaining <SearchValidation>();
            });

            ////check and del if it doesnt help
            //services.Configure<ApiBehaviorOptions>(options =>
            //{
            //    options.InvalidModelStateResponseFactory = context =>
            //    {
            //        var problemDetails = new ValidationProblemDetails(context.ModelState)
            //        {
            //            Instance = context.HttpContext.Request.Path,
            //            Status = StatusCodes.Status400BadRequest,
            //            Type = "https://asp.net/core",
            //            Detail = "Please refer to the errors property for additional details."
            //        };
            //        return new BadRequestObjectResult(problemDetails)
            //        {
            //            ContentTypes = { "application/problem+json", "application/problem+xml" }
            //        };
            //    };
            //});



            services.AddAutoMapper(typeof(Startup).Assembly);

            services.AddCors(options =>
            {
                options.AddPolicy("CorsPolicy",
                                  builder => builder.WithOrigins("http://localhost:4200") //+url host-a
                                  .AllowAnyMethod()
                                  .AllowAnyHeader()
                                  .AllowCredentials()
                                  .SetIsOriginAllowedToAllowWildcardSubdomains());
            });

            services.AddSignalR();
        }
        public override bool Equals(object obj)
        {
            PhotobucketToken token = obj as PhotobucketToken;

            return(Token.Equals(token.Token) && TokenSecret.Equals(token.TokenSecret));
        }