asp.net-core - 添加更新和删除产品方法在邮递员中不起作用并且不在我的数据库中进行任何更改
问题描述
services.AddAuthorization(options =>
{
options.AddPolicy("RequireLoggedIn", policy => policy.RequireRole("Admin", "Customer", "Moderator").RequireAuthenticatedUser());
options.AddPolicy("RequireAdministratorRole", policy => policy.RequireRole("Admin").RequireAuthenticatedUser());
});
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using NG_Core_Auth.Data;
using NG_Core_Auth.Models;
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
namespace NG_Core_Auth.Controllers
{
[Route("api/[controller]")]
public class ProductController : Controller
{
private readonly ApplicationDbContext _db;
public ProductController(ApplicationDbContext db)
{
_db = db;
}
// GET: api/values
[HttpGet("[action]")]
[Authorize(Policy = "RequireLoggedIn")]
public IActionResult GetProducts()
{
return Ok(_db.Products.ToList());
}
[HttpPost("[action]")]
[Authorize(Policy = "RequireAdministratorRole")]
public async Task<IActionResult> AddProduct([FromBody] ProductModel formdata)
{
var newproduct = new ProductModel
{
Name = formdata.Name,
ImageUrl = formdata.ImageUrl,
Description = formdata.Description,
OutOfStock = formdata.OutOfStock,
Price = formdata.Price
};
await _db.Products.AddAsync(newproduct);
await _db.SaveChangesAsync();
return Ok(new JsonResult("The Product was Added Successfully"));
}
[HttpPut("[action]/{id}")]
[Authorize(Policy = "RequireAdministratorRole")]
public async Task<IActionResult> UpdateProduct([FromRoute] int id, [FromBody] ProductModel formdata)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var findProduct = _db.Products.FirstOrDefault(p => p.ProductId == id);
if (findProduct == null)
{
return NotFound();
}
// If the product was found
findProduct.Name = formdata.Name;
findProduct.Description = formdata.Description;
findProduct.ImageUrl = formdata.ImageUrl;
findProduct.OutOfStock = formdata.OutOfStock;
findProduct.Price = formdata.Price;
_db.Entry(findProduct).State = EntityState.Modified;
await _db.SaveChangesAsync();
return Ok(new JsonResult("The Product with id " + id + " is updated"));
}
[HttpDelete("[action]/{id}")]
[Authorize(Policy = "RequireAdministratorRole")]
public async Task<IActionResult> DeleteProduct([FromRoute] int id)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
// find the product
var findProduct = await _db.Products.FindAsync(id);
if (findProduct == null)
{
return NotFound();
}
_db.Products.Remove(findProduct);
await _db.SaveChangesAsync();
// Finally return the result to client
return Ok(new JsonResult("The Product with id " + id + " is Deleted."));
}
}
}
我使用添加更新和删除方法运行控制器我运行应用程序并使用https://localhost:44301/ url 成功运行然后我通过添加https://localhost:44301/api/product在邮递员中正确执行所有步骤/addproduct,放置标题并将我要插入的数据放入正文行并使用 post 方法单击发送返回 401 Unauthorized,有什么问题?我发布了 Productcontroller.cs 和部分 startup.cs 文件
解决方案
尝试先在 Postman 中对您的登录端点进行 POST。这会将 cookie 提供给您已登录的 Postman(以及您拥有的角色)。然后接下来您应该能够使用Authorize
属性成功发布到您的端点
推荐阅读
- video - Safari 11.1.2 将无法播放视频 django 应用程序
- junit - JUnit 在继续之前等待
- sql - 如何从记录中选择信息,以及之后没有出现的第一条记录
- xml - 在 PowerShell 中添加同名的 XML 元素
- report - Stimulsoft 报告需要在保存按钮后刷新
- java - 使用 Java 中的 Kafka Streams 从 Kafka 主题读取和写入 JSON 字符串
- rest - Web服务:一个数据库获得两个主要版本
- ruby - 应用程序未获取 /usr/local/bin 路径可执行文件
- git - gitlab git lfs + apache2,推送文件时得到 502“AH01097: pass request body failed to 127.0.0.1:8181”
- android - Android minifyEnabled true 在调试和发布模式下不起作用