curl -X POST -H 'Content-Type:ap-6ren">
gpt4 book ai didi

api - curl :错误:(3)端口号以 '"结尾 '

转载 作者:可可西里 更新时间:2023-11-01 15:08:04 25 4
gpt4 key购买 nike

我正在尝试获取 dot net core 2.0 web api 的 token 。

这就是我正在做的:

C:\Users\danyb>curl -X POST -H 'Content-Type:application/json'^
Mehr? -d '{\"username\":\"mario\",\"password\":\"secret\"}'^
Mehr? localhost:56183/api/token

[1/2]: '"username":"mario"'localhost:56183/api/token --> <stdout>
--_curl_--'"username":"mario"'localhost:56183/api/token
curl: (3) Port number ended with '"'

[2/2]: '"password":"secret"'localhost:56183/api/token --> <stdout>
--_curl_--'"password":"secret"'localhost:56183/api/token
curl: (3) Port number ended with '"'

我已经在网上搜索过,但找不到可行的解决方案。

token Controller 类:

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.IdentityModel.Tokens;
using System;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;

namespace JWT.Controllers
{
[Route("api/[controller]")]
public class TokenController : Controller
{
private IConfiguration _config;

public TokenController(IConfiguration config)
{
_config = config;
}

[AllowAnonymous]
[HttpPost]
public IActionResult CreateToken([FromBody]LoginModel login)
{
IActionResult response = Unauthorized();
var user = Authenticate(login);

if (user != null)
{
var tokenString = BuildToken(user);
response = Ok(new { token = tokenString });
}

return response;
}

private string BuildToken(UserModel user)
{

var claims = new[] {
new Claim(JwtRegisteredClaimNames.Sub, user.Name),
new Claim(JwtRegisteredClaimNames.Email, user.Email),
new Claim(JwtRegisteredClaimNames.Birthdate, user.Birthdate.ToString("yyyy-MM-dd")),
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString())
};

var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["Jwt:Key"]));
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);

var token = new JwtSecurityToken(_config["Jwt:Issuer"],
_config["Jwt:Issuer"],
claims,
expires: DateTime.Now.AddMinutes(30),
signingCredentials: creds);

return new JwtSecurityTokenHandler().WriteToken(token);
}

private UserModel Authenticate(LoginModel login)
{
UserModel user = null;

if (login.Username == "mario" && login.Password == "secret")
{
user = new UserModel { Name = "Mario Rossi", Email = "mario.rossi@domain.com" };
}
return user;
}

public class LoginModel
{
public string Username { get; set; }
public string Password { get; set; }
}

private class UserModel
{
public string Name { get; set; }
public string Email { get; set; }
public DateTime Birthdate { get; set; }
}
}
}

我认为该错误与 Controller 无关,而与 Curl 调用本身有关。

最佳答案

我遇到了类似的事情,被一些特殊字符单引号和双引号骗了。所以我在这里的建议是,确保您在命令行上正确设置了 curl 请求的格式。如果您坚持在单引号内使用双引号,则无需转义。

试试这个:

curl -X POST -H 'Content-Type:application/json' http://localhost:56183/api/token -d '{"username":"mario", "password":"secret"}'

关于api - curl :错误:(3)端口号以 '"结尾 ',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50784321/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com