using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authorization; using AuthStudy.WebApp.VModels; namespace AuthStudy.WebApp.Controllers { [Route("api/[controller]/[action]")] [ApiController] public class AccountsController : ControllerBase { private readonly ILogger _logger; public AccountsController(ILogger logger) { _logger = logger; } //多特性是 and, 特性内逗号分隔是or [Authorize] //[Authorize(AuthenticationSchemes = AuthenticationSchemeList.BaseBrowserScheme)] //[Authorize(AuthenticationSchemes = AuthenticationSchemeList.BrowserScheme)] //[Authorize(AuthenticationSchemes = AuthenticationSchemeList.BasicScheme)] //[Authorize(AuthenticationSchemes = $"{AuthenticationSchemeList.BrowserScheme},{AuthenticationSchemeList.BasicScheme}")] //[Authorize(AuthenticationSchemes = $"{AuthenticationSchemeList.BaseBrowserScheme},{AuthenticationSchemeList.BrowserScheme},{AuthenticationSchemeList.BasicScheme}")] [HttpGet] public async Task GetAll() { // var authenticateResult = await HttpContext.AuthenticateAsync(); // if (authenticateResult.Succeeded) // { // _logger.LogInformation("认证成功"); // } // else // { // Response.StatusCode = 401; // _logger.LogInformation("认证失败"); // return new ContentResult() { StatusCode = 401,Content=authenticateResult.Failure?.Message}; // } //输出认证信息 foreach (var claim in User.Claims) { _logger.LogInformation($"{claim.Type}={claim.Value}"); } List accounts = new() { new AccountVM(){ Name="张三", Email="zhangsan@qq.com", Password="123456"}, new AccountVM(){ Name="小明", Email="xiaoming@qq.com", Password="123456"}, new AccountVM(){ Name="癫子", Email="dianzi@qq.com", Password="123456"} }; //授权 var authorazitionService = HttpContext.RequestServices.GetService(); Task? authResult = authorazitionService?.AuthorizeAsync(HttpContext.User, "DefaultPolicy"); var dd = await authResult; return new JsonResult(accounts); } [Authorize( AuthenticationSchemes = $"{AuthenticationSchemeList.BrowserScheme},{AuthenticationSchemeList.BaseBrowserScheme}", Policy = "DefaultPolicy", Roles = "Admin,User" )] [HttpGet] public async Task GetFirst() { var o = HttpContext.RequestServices.GetService>(); _logger.LogInformation($"默认全局认证方案:{o?.Value.DefaultScheme},当前默认方案{o?.Value.DefaultAuthenticateScheme}"); var authenticateResult = await HttpContext.AuthenticateAsync(); if (authenticateResult.Succeeded) { _logger.LogInformation("认证成功"); } else { Response.StatusCode = 401; _logger.LogInformation("认证失败"); return new ContentResult() { StatusCode = 401,Content=authenticateResult.Failure?.Message}; } return new JsonResult(new AccountVM(){ Name="张三", Email="zhangsan@qq.com", Password="123456"} ); } /// /// 手动认证与授权 /// [HttpGet] public async Task GetLast() { var o = HttpContext.RequestServices.GetService>(); _logger.LogInformation($"默认全局认证方案:{o?.Value.DefaultScheme},当前默认方案{o?.Value.DefaultAuthenticateScheme}"); var authenticateResult = await HttpContext.AuthenticateAsync(); if (authenticateResult.Succeeded) { _logger.LogInformation("认证成功"); } else { Response.StatusCode = 401; _logger.LogInformation("认证失败"); return new ContentResult() { StatusCode = 401,Content=authenticateResult.Failure?.Message}; } return new JsonResult(new AccountVM(){ Name="丁聪", Email="dingding@qq.com", Password="123456"} ); } [HttpPost] public IActionResult Login(string loginName, string loginPassword) { var info = new { Name = loginName, Roles = "Admin" }; return new JsonResult(info); } } }