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.

68 lines
2.5 KiB
C#

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<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"}
};
return new JsonResult(accounts);
}
[HttpPost]
public IActionResult Login(string LoginName, string LoginPassword)
{
var info = new { Name = LoginName, Roles = "Admin" };
return new JsonResult(info);
}
}
}