.net core 学习小结之 PostMan报415

news/2025/2/23 9:34:47
  • 首先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 == "123456")
                    {
                        //用户合法情况
                        //添加授权信息
                        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(30),//过期时间
                            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]

转载于:https://www.cnblogs.com/chongyao/p/8652743.html


http://www.niftyadmin.cn/n/1825715.html

相关文章

关于pandas精度控制

最近使用pandas处理一批数据&#xff0c;数据中包含几个columns&#xff0c;它们的数据精度&#xff0c;例如 3.25165&#xff0c;1451684684168.0&#xff0c;0.23 处理完之后保存csv时发现&#xff0c;1451684684168.0被保存为1.45168e12&#xff0c;我需要完全保存数据信息 …

01_搭建Linux虚拟机(上)_我的Linux之路

转载自特克斯博客www.susmote.com 一般我们学习Linux都是搭建一个虚拟机环境&#xff0c;当然你也可以装一个Linux系统&#xff08;配上windows做一个双系统主机&#xff09;&#xff0c;我个人认为初学者还是先选择搭建一个虚拟机来学习Linux&#xff08;或者可以说是玩&#…

C++拓展笔记1-2:浅谈C++编程中可能会用到的预处理指令

为什么80%的码农都做不了架构师&#xff1f;>>> 一、#include预处理指令 #include指令一般用于包含本文件中需要用到的其它程序片段。有#include<filename>和 #include "filename"这两种形式。第一种形式包括的是在C标准库&#xff08;STL&#xf…

【BZOJ4870】组合数问题 [矩阵乘法][DP]

组合数问题 Time Limit: 10 Sec Memory Limit: 512 MB[Submit][Status][Discuss]Description Input 第一行有四个整数 n, p, k, r&#xff0c;所有整数含义见问题描述。Output 一行一个整数代表答案。Sample Input 2 10007 2 0Sample Output 8HINT 1 ≤ n ≤ 10^9, 0 ≤ r <…

Python 再谈字符串

字符串除了要用引号来创建之外&#xff0c;其他和元组一样&#xff0c;不能修改&#xff0c;如果要修改只能用切片或者拼接的方式。其他的什么乱七八糟的运算符都一样一些不同 capitalize()—将字符串的第一个字母大写str1.capitalize() casefold()—将字符串的所有字母小写str…

cassandra——可以预料的查询,如果你的查询条件有一个是根据索引查询,那其它非索引非主键字段,可以通过加一个ALLOW FILTERING来过滤实现...

cassandra的索引查询和排序 转自&#xff1a;http://zhaoyanblog.com/archives/499.htmlcassandra的索引查询和排序 cassandra的查询虽然很弱&#xff0c;但是它也是支持索引和排序的&#xff0c;当然是简陋的查询&#xff0c;这一切都是为了追求性能的代价&#xff0c;所以要使…

Java内部类的使用总结

2019独角兽企业重金招聘Python工程师标准>>> 内部类是指在一个外部类的内部再定义一个类。类名不需要和文件夹相同。 *内部类可以是静态static的&#xff0c;也可用public&#xff0c;default&#xff0c;protected和private修饰。&#xff08;而外部顶级类即类名和…

【Nginx】Openresty增加waf配置

1. Ngx lua waf 说明 防止sql注入&#xff0c;本地包含&#xff0c;部分溢出&#xff0c;fuzzing测试&#xff0c;xss,SSRF等web攻击防止svn/备份之类文件泄漏防止ApacheBench之类压力测试工具的攻击屏蔽常见的扫描黑客工具&#xff0c;扫描器屏蔽异常的网络请求屏蔽图片附件类…