• 首先415的官方解释是:对于当前请求的方法和所请求的资源,请求中提交的实体并不是服务器中所支持的格式,因此请求被拒绝。
  • 也就是说我所准备的数据格式并不是后台代码使用的数据格式
  • 后台代码如下
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc; namespace JwtAuth.Controllers
    {
    using System.Security.Claims;
    using Microsoft.Extensions.Options;
    using Microsoft.IdentityModel.Tokens;
    using Microsoft.AspNetCore.Authentication.JwtBearer;
    //添加dll的引用 Nuget Microsoft.AspNetCore.Authentication.JwtBearer;
    using System.IdentityModel.Tokens.Jwt;
    [Route("api/[controller]")]
    public class AuthController : Controller
    {
    public JwtSettings settings;
    public AuthController(IOptions<JwtSettings> jwtsettings)
    {
    settings = jwtsettings.Value;
    }
    [HttpPost]
    public IActionResult Token([FromBody]LoginInfo model)
    {
    if (ModelState.IsValid)
    {
    if (model.username == "cyao" && model.password == "")
    {
    //用户合法情况
    //添加授权信息
    var claims = new Claim[] { new Claim(ClaimTypes.Name, "cyao"), new Claim(ClaimTypes.Role, "admin") };
    var key = new SymmetricSecurityKey(System.Text.Encoding.UTF8.GetBytes(settings.SecretKey));
    var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
    var token = new JwtSecurityToken(
    settings.Issuer,
    settings.Audience,
    claims,
    DateTime.Now,
    DateTime.Now.AddMinutes(),//过期时间
    creds);
    return Ok(new { token = new JwtSecurityTokenHandler().WriteToken(token) });
    }
    }
    return BadRequest();
    }
    }
    public class LoginInfo
    {
    [Required]
    public string username { get; set; }
    [Required]
    public string password { get; set; }
    }
    }
  • 使用POSTMan如何构造一个
    [FromBody]?错误示例(图1.0)
  • 正确示例如下图2.0(图2.0)
  • 或者使用图1.0的配置将后台代码参数的标签改成[FromForm]

.net core 学习小结之 PostMan报415的更多相关文章

  1. .net core 学习小结之环境配置篇

    安装IIs对 netcore 的支持 https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/aspnet-core-mod ...

  2. .net core 学习小结之 自定义JWT授权

    自定义token的验证类 using System; using System.Collections.Generic; using System.IO; using System.Linq; usi ...

  3. .net core 学习小结之 JWT 认证授权

    新增配置文件 { "Logging": { "IncludeScopes": false, "Debug": { "LogLeve ...

  4. .net core 学习小结之 Cookie-based认证

    在startup中添加授权相关的管道 using System; using System.Collections.Generic; using System.Linq; using System.T ...

  5. .net core 学习小结之 配置介绍(config)以及热更新

    命令行的配置 var settings = new Dictionary<string, string>{ { "name","cyao"}, {& ...

  6. objective-c基础教程——学习小结

    objective-c基础教程——学习小结   提纲: 简介 与C语言相比要注意的地方 objective-c高级特性 开发工具介绍(cocoa 工具包的功能,框架,源文件组织:XCode使用介绍) ...

  7. SpringMVC过程中@RequestBody接收Json的问题 总是报415

    在SpringMVC中用@RequestBody接收Json的问题,总是报415,经过一翻查找 前台js的post: var postdata = '{"title":" ...

  8. EntityFramework Core 学习系列(一)Creating Model

    EntityFramework Core 学习系列(一)Creating Model Getting Started 使用Command Line 来添加 Package  dotnet add pa ...

  9. python --- 字符编码学习小结(二)

    距离上一篇的python --- 字符编码学习小结(一)已经过去2年了,2年的时间里,确实也遇到了各种各样的字符编码问题,也能解决,但是每次都是把所有的方法都试一遍,然后终于正常.这种方法显然是不科学 ...

随机推荐

  1. 约会 Rendezvous:基环树

    提炼:tarjan判环,dfs建树,倍增lca,预处理环两点间距离 我犯的错误: 1.基环树不只有一棵,可以有很多 2.自环不能将其忽略,(对于我的算法)应该将其特殊考虑在算法内 3.代码一定要简洁有 ...

  2. VxLAN、PAE、Telemetry简介

    VxLAN VxLAN协议将  Ethernet帧  封装在UDP内,再加上8个字节的VXLAN header,用来标识不同的二层网络. VxLAN的角度看网络虚拟化:在一套物理网络设备上虚拟出多个二 ...

  3. LAMP 系统性能调优之网络文件系统调优

    LAMP 系统性能调优之网络文件系统调优 2011-03-21 09:35 Sean A. Walberg 网络转载 字号:T | T 使用LAMP系统的用户,都想把自己LAMP性能提高运行的速度提高 ...

  4. python 习题

    文件内容为一个多层元组,遍历该元组,当全为数字时输出数字之和,全为字母输出字符串,有数字有字母输出False,并将该内容写入到该文件的下一行中 # 方法一: t1= ((1,2,3),("a ...

  5. int 和guid做主键的时候性能的区别

    1.在经常需要做数据迁移的系统中,建议用Guid.并且在相应的外键字段,也就是用来做连接查询的字段添加非聚集索引,对于改善性能有极大的好处.where条件的字段也可以适当添加非聚集索引. 2.在使用G ...

  6. ideal 工具jdk环境配置

    1.File  >>  Other Settings >> Default Project Structure ... 2.Project  >>  jdk_vie ...

  7. 泛型(三)模拟commons-dbutils

    最近在复习泛型的知识,想起以前使用commons-dbutils的时候,觉得这个工具太厉害了.所以,试着自己瞎写看能不能模拟commons-dbutils的功能. 1.commons-dbutils的 ...

  8. Python抽象类(abc模块)

    1.抽象类概念 抽象类是一个特殊的类,只能被继承,不能实例化 2.为什么要有抽象类 其实在未接触抽象类概念时,我们可以构造香蕉.苹果.梨之类的类,然后让它们继承水果这个基类,水果的基类包含一个eat函 ...

  9. Java+超大文件上传

    之前仿造uploadify写了一个HTML5版的文件上传插件,没看过的朋友可以点此先看一下~得到了不少朋友的好评,我自己也用在了项目中,不论是用户头像上传,还是各种媒体文件的上传,以及各种个性的业务需 ...

  10. sh_01_九九乘法表

    sh_01_九九乘法表 def multiple_table(): # 1. 打印 9 行小星星 row = 1 while row <= 9: col = 1 while col <= ...