未授予所需的权限。必须至少授予以下权限之一:用户

Required permissions are not granted. At least one of these permissions must be granted: Users

我正在尝试在 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 上实现文件上传?

您正在注入 IUserAppService 并且 its implementation 需要 PermissionNames.Pages_Users:

[AbpAuthorize(PermissionNames.Pages_Users)]
public class UserAppService : AsyncCrudAppService<...>, IUserAppService

这些是您的选择:

  1. BacklogController 中删除 IUserAppService 的注入,因为您没有使用它。

    // private readonly IUserAppService _userAppService;
    private readonly BacklogAppService _backlogAppService;
    
    // public BacklogController(IUserAppService userAppService, BacklogAppService backlogAppService)
    public BacklogController(BacklogAppService backlogAppService)
    {
        // _userAppService = userAppService;
        _backlogAppService = backlogAppService;
    }
    
  2. 以租户管理员身份登录,默认授予 PermissionNames.Pages_Users

  3. 向您登录的用户授予 PermissionNames.Pages_Users