using AuthStudy.Authentication.Browser; using AuthStudy.WebApp.VModels; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; namespace AuthStudy.WebApp.Controllers { [Route("api/[controller]/[action]")] [ApiController] public class AccountsController : ControllerBase { private 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"} }; return new JsonResult(accounts); } [HttpPost] public IActionResult Login(string LoginName, string LoginPassword) { var info = new { Name = LoginName, Roles = "Admin" }; return new JsonResult(info); } } }