You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
4.9 KiB
C#

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<AccountsController> _logger;
public AccountsController(ILogger<AccountsController> 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<IActionResult> 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<AccountVM> 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<IAuthorizationService>();
Task<AuthorizationResult>? 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<IActionResult> GetFirst()
{
var o = HttpContext.RequestServices.GetService<IOptions<AuthenticationOptions>>();
_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"} );
}
/// <summary>
/// 手动认证与授权
/// </summary>
[HttpGet]
public async Task<IActionResult> GetLast()
{
var o = HttpContext.RequestServices.GetService<IOptions<AuthenticationOptions>>();
_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);
}
}
}