[ApiController] [Route("[controller]")]publicclassTestController : ControllerBase {privatereadonlyIConfiguration _configuration;publicTestController(IConfiguration configuration)=>_configuration =configuration; [HttpPost("GenerateHashedPwd")]publicstringGenerate([FromBody]stringplainPassword) {varpasswordHasher=...
此API 控制器中的 [Authorize] 屬性是保護此 API 免於未經授權存取的唯一屬性。 在[Authorize] 應用程式中使用的 Blazor WebAssembly 只是作為一個提示,指出使用者應獲得授權才能讓應用程式正常運行。 C# 複製 [Authorize] [ApiController] [Route("[controller]")] [RequiredScope(RequiredScopesConfigurationKey =...
现在,我们应该能够将基于角色的授权应用到我们的应用程序中。我们来关注下API的处理。 6|0将基于角色的授权应用于API 将WeatherForecastController上的Get方法设置为仅对Admin角色中经过身份验证的用户可访问。我们使用Authorize属性并指定用于访问它的角色。(这里在默认生成模版与原文有出入) [HttpGet][Authorize(Roles...
VS创建一个ASP.net core Host的Blazor wsam解决方案,其中 Server端项目即包含了未受保护的 WeatherForecast API接口. 稍微讲解一下 ASP.Net Core API的路由规则. 下面代码是模板自动生成的, Route 注解中的参数是 [controller], HttpGet 注解没带参数, 则该方法的url为 http://site/WeatherForecast, VS 插件 R...
接下来,我向 API 项目添加新控制器。新控制器接受来自 Blazor 客户端的 RegistrationData 调用,如图 9 所示。注册控制器在服务器上运行,并且是后端 API 服务器的典型特征。区别在于,它现在运行在客户端上运行的相同验证规则。 图9:注册控制器 C# 复制 [Route("api/Registration")] [ApiController]...
In BlazorFirstSteps.Server project, we delete the WeatherForecastController.cs file from Controllers folder. In BlazorFirstSteps.Shared project, we delete WeatherForecast.cs file. It might be a good idea to remove unused usings for every C# file, which you are going to use. Making the New ...
Controllers/FilesaveController.cs: C# 複製 using System; using System.Collections.Generic; using System.IO; using System.Net; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using BlazorSample.Shared; [ApiController] ...
Applying role-based authorization to the API Let’s set theWeatherForecastaction on theSampleDataControllerto only be accessible to authenticated users in theAdminrole. We do this by using theAuthorizeattribute and specifying the roles that are allowed to access it. ...
using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using BlazingPizza.Data; namespace BlazingPizza.Controllers; [Route("specials")] [ApiController] public class SpecialsController : Controller { private readonly PizzaStoreContext _db; public SpecialsController(PizzaSt...
现在我们已经有一些可用的角色了,我们现在来更新账户控制器(Accounts controller)创建用户的动作。 在新增用户时候为其分配User角色。如果新用户的电子邮件以admin开头,则为其分配User和Admin角色组。 [HttpPost] publicasyncTask<IActionResult>Post([FromBody]RegisterModelmodel){ ...