未授予所需权限。必须至少授予其中一个权限:用户

问题描述 投票:0回答:1

我正在尝试在ASP.NET Boilerplate项目中实现文件上载。这是我的代码:

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();
        }
    }
}

Web应用程序运行,但是当我点击上传按钮时,它会说:

未授予必要的授权。必须至少授予其中一个权限:用户

我在哪里做错了?否则,是否有更简单的方法在ASP.NET Boilerplate上实现文件上载?

c# asp.net-core dependency-injection authorization aspnetboilerplate
1个回答
0
投票

你注射IUserAppServiceits implementation需要PermissionNames.Pages_Users

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

这些是你的选择:

  1. IUserAppService中取出注射BacklogController,因为你没有使用它。 // 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授予您已登录的用户。
© www.soinside.com 2019 - 2024. All rights reserved.