2

因此,我将我的 RestAPI 项目从 ASP.NET Core 2.1 迁移到 ASP.NET Core 3.0,并且HttpPost之前工作的功能停止工作。

    [AllowAnonymous]
    [HttpPost]
    public IActionResult Login([FromBody]Application login)
    {
        _logger.LogInfo("Starting Login Process...");

        IActionResult response = Unauthorized();
        var user = AuthenticateUser(login);

        if (user != null)
        {
            _logger.LogInfo("User is Authenticated");
            var tokenString = GenerateJSONWebToken(user);

            _logger.LogInfo("Adding token to cache");
            AddToCache(login.AppName, tokenString);                

            response = Ok(new { token = tokenString });

            _logger.LogInfo("Response received successfully");
        }

        return response;
    }

现在,登录对象的每个属性都有空值。我在这里读到

默认情况下,当您在 Startup.cs 中调用 AddMvc() 时,会自动配置 JSON 格式化程序 JsonInputFormatter,但您可以根据需要添加其他格式化程序,例如将 XML 绑定到对象。

由于在 aspnetcore 3.0 中删除了 AddMvc,现在我觉得这就是我无法再获取我的 JSON 对象的原因。我的启动类配置功能如下所示:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
       if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseHsts();
        }
        app.UseHttpsRedirection();
        app.UseAuthentication();
        app.UseRouting();
        //app.UseAuthorization();
        //app.UseMvc(options
        //    /*routes => {
        //    routes.MapRoute("default", "{controller=Values}/{action}/{id?}");
        //}*/);
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
            endpoints.MapRazorPages();
        });
    }

调试日志对象

我通过邮递员发送的请求(选择了原始和 JSON 选项)

{ "AppName":"XAMS", "licenseKey": "XAMSLicenseKey" }

更新

邮递员标头:内容类型:应用程序/json

启动.cs

public void ConfigureServices(IServiceCollection services)
    {            
        //_logger.LogInformation("Starting Log..."); //shows in output window

        services.AddSingleton<ILoggerManager, LoggerManager>();

        services.AddMemoryCache();
        services.AddDbContext<GEContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
        //services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
        services.AddControllers();
        services.AddRazorPages();

        //Authentication
        services.AddAuthentication(options =>
        {
            options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
            options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
        }).AddJwtBearer(options =>
        {
            options.Authority = "https://localhost:44387/";
            options.Audience = "JWT:Issuer";
            options.TokenValidationParameters.ValidateLifetime = true;
            options.TokenValidationParameters.ClockSkew = TimeSpan.FromMinutes(5);
            options.RequireHttpsMetadata = false;
        });

        services.AddAuthorization(options =>
        {
            options.AddPolicy("GuidelineReader", p => {
                p.RequireClaim("[url]", "GuidelineReader");
            });
        });
        //
    }

应用程序.cs

 public class Application
 {
    public string AppName;
    public string licenseKey;
 }
4

1 回答 1

3

随着您更新代码,我认为原因是您没有setter为您的属性创建。

Application要解决此问题,请按如下方式更改您的模型:

公开课申请
{
    公共字符串 AppName   {get;set;}
    公共字符串 licenseKey {get;set;}
}
于 2019-10-23T05:00:17.100 回答