我正在尝试在 ASP.NET 样板项目中实现文件上传。 这是我的代码:
Index.cshtml:
<form asp-controller="Backlog" asp-action="Upload_Image" method="post"
enctype="multipart/form-data">
<input type="file" name="file" />
<button type="submit">Upload Image</button>
</form>
BacklogController.cs:
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Abp.Application.Services.Dto;
using Abp.AspNetCore.Mvc.Authorization;
using MyProject.Authorization;
using MyProject.Controllers;
using MyProject.Users;
using MyProject.Web.Models.Backlog;
using MyProject.Users.Dto;
using System.Collections.Generic;
using MyProject.Backlog;
namespace MyProject.Controllers
{
[AbpMvcAuthorize(PermissionNames.Pages_Backlog)]
public class BacklogController : MyProjectControllerBase
{
private readonly IUserAppService _userAppService;
private readonly BacklogAppService _backlogAppService;
public BacklogController(IUserAppService userAppService, BacklogAppService backlogAppService)
{
_userAppService = userAppService;
_backlogAppService = backlogAppService;
}
public async Task<ActionResult> Index()
{
var backlogItems = (await _backlogAppService.GetBackLogItems()).Items;
var model = new BacklogListViewModel
{
BacklogItems = backlogItems
};
return View(model);
}
[HttpPost] // Postback
public async Task<IActionResult> Upload_Image(IFormFile file)
{
if (file == null || file.Length == 0) return Content("file not selected");
return View();
}
}
}
网络应用程序运行,但是当我点击上传按钮时,它说:
the necessary authorizations are not granted. At least one of these permissions must be granted: Users
我哪里做错了?否则,是否有更简单的方法在 ASP.NET Boilerplate 上实现文件上传?
最佳答案
您正在注入(inject) IUserAppService
和 its implementation需要 PermissionNames.Pages_Users
:
[AbpAuthorize(PermissionNames.Pages_Users)]
public class UserAppService : AsyncCrudAppService<...>, IUserAppService
这些是您的选择:
从
BacklogController
中移除IUserAppService
的注入(inject),因为您没有使用它。// private readonly IUserAppService _userAppService; private readonly BacklogAppService _backlogAppService; // public BacklogController(IUserAppService userAppService, BacklogAppService backlogAppService) public BacklogController(BacklogAppService backlogAppService) { // _userAppService = userAppService; _backlogAppService = backlogAppService; }
以租户管理员身份登录,默认授予
PermissionNames.Pages_Users
。- 向您已登录的用户授予
PermissionNames.Pages_Users
。
关于c# - 未授予所需的权限。必须至少授予其中一项权限 : Users,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55107976/