代码之家  ›  专栏  ›  技术社区  ›  bbqchickenrobot joe_coolish

基于JWT的API+Piranha CMS劫持[授权]路线

  •  0
  • bbqchickenrobot joe_coolish  · 技术社区  · 7 年前

    最近在Piranha项目中为API设置了JWT。我可以点击登录端点(匿名),而不用食人鱼劫持请求。

    当我使用[authorize]属性到达API端点(成功授权和接收JWT之后)时,它总是被食人鱼捕获。它试图将我重定向到CMS登录。

    由于这是一个API,重定向到网页是不可接受的行为。无论如何要纠正这种行为?

            var appSettingsSection = config.GetSection("AppSettings");
            services.Configure<AppSettings> (appSettingsSection);
            // configure jwt authentication
            var appSettings = appSettingsSection.Get<AppSettings> ();
            var key = Encoding.UTF8.GetBytes (appSettings.Secret); // todo - UTF8 vs ASCII?!
            services.AddAuthentication (x => {
                    x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                    x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
                })
                .AddJwtBearer (x => {
                    x.RequireHttpsMetadata = false;
                    x.SaveToken = true;
                    x.TokenValidationParameters = new TokenValidationParameters {
                        ValidateIssuerSigningKey = true,
                        IssuerSigningKey = new SymmetricSecurityKey (key),
                        ValidateIssuer = false,
                        ValidateAudience = false
                    };
                });
            services.AddPiranhaApplication ();
            services.AddPiranhaFileStorage ();
            services.AddPiranhaImageSharp ();
    
                services.AddPiranhaEF (options =>
                    options.UseSqlite ("Filename=./piranha.db"));
                services.AddPiranhaIdentityWithSeed<IdentitySQLiteDb> (options =>
                    options.UseSqlite ("Filename=./piranha.db"));
            }
            services.AddPiranhaManager ();
            services.AddPiranhaMemCache ();
    
            services.AddMvc (config => {
                    config.ModelBinderProviders.Insert (0,
                        new Piranha.Manager.Binders.AbstractModelBinderProvider ());
                }).SetCompatibilityVersion (CompatibilityVersion.Version_2_1);
    

    -------更新-------- 在@hakan的帮助下,以下属性起作用:

    [ApiController]
    [Route ("api/v1/")]
    [Produces("application/json")]
    [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
    public class ApiController : ControllerBase {
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Håkan Edling    7 年前

    这里的问题实际上是ASP.NET标识如何与JWT交互。在启动时,您的呼叫:

    services.AddPiranhaIdentityWithSeed<IdentitySQLiteDb> (options =>
        options.UseSqlite ("Filename=./piranha.db"));
    

    这意味着安装程序使用默认选项piranha集,其中一些选项实际上更倾向于开发(如密码强度)。你可以自己提供 options cookie options 进入方法,就像这样:

    services.AddPiranhaIdentityWithSeed<IdentitySQLiteDb> (options =>
        options.UseSqlite ("Filename=./piranha.db"), identityOptions, cookieOptions);
    

    使用的默认标识选项是:

    // Password settings
    options.Password.RequireDigit = false;
    options.Password.RequiredLength = 6;
    options.Password.RequireNonAlphanumeric = false;
    options.Password.RequireUppercase = false;
    options.Password.RequireLowercase = false;
    options.Password.RequiredUniqueChars = 1;
    
    // Lockout settings
    options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(30);
    options.Lockout.MaxFailedAccessAttempts = 10;
    options.Lockout.AllowedForNewUsers = true;
    
    // User settings
    options.User.RequireUniqueEmail = true;
    

    这些是默认的cookie选项:

    options.Cookie.HttpOnly = true;
    options.ExpireTimeSpan = TimeSpan.FromMinutes(30);
    options.LoginPath = "/manager/login";
    options.AccessDeniedPath = "/manager/login";
    options.SlidingExpiration = true;
    

    最好的问候

    河南菅

    推荐文章