|
|
|
@ -1,10 +1,9 @@
|
|
|
|
|
using AuthStudy.Authentication.Browser;
|
|
|
|
|
using AuthStudy.WebApp.VModels;
|
|
|
|
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
using Microsoft.AspNetCore.Authentication;
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
|
|
using AuthStudy.WebApp.VModels;
|
|
|
|
|
|
|
|
|
|
namespace AuthStudy.WebApp.Controllers
|
|
|
|
|
{
|
|
|
|
@ -12,13 +11,13 @@ namespace AuthStudy.WebApp.Controllers
|
|
|
|
|
[ApiController]
|
|
|
|
|
public class AccountsController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
private ILogger<AccountsController> _logger;
|
|
|
|
|
private readonly ILogger<AccountsController> _logger;
|
|
|
|
|
public AccountsController(ILogger<AccountsController> logger)
|
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//多特性是and特性内逗号分隔是or
|
|
|
|
|
//多特性是 and, 特性内逗号分隔是or
|
|
|
|
|
[Authorize]
|
|
|
|
|
//[Authorize(AuthenticationSchemes = AuthenticationSchemeList.BaseBrowserScheme)]
|
|
|
|
|
//[Authorize(AuthenticationSchemes = AuthenticationSchemeList.BrowserScheme)]
|
|
|
|
@ -26,7 +25,7 @@ namespace AuthStudy.WebApp.Controllers
|
|
|
|
|
//[Authorize(AuthenticationSchemes = $"{AuthenticationSchemeList.BrowserScheme},{AuthenticationSchemeList.BasicScheme}")]
|
|
|
|
|
//[Authorize(AuthenticationSchemes = $"{AuthenticationSchemeList.BaseBrowserScheme},{AuthenticationSchemeList.BrowserScheme},{AuthenticationSchemeList.BasicScheme}")]
|
|
|
|
|
[HttpGet]
|
|
|
|
|
public IActionResult GetAll()
|
|
|
|
|
public async Task<IActionResult> GetAll()
|
|
|
|
|
{
|
|
|
|
|
// var authenticateResult = await HttpContext.AuthenticateAsync();
|
|
|
|
|
// if (authenticateResult.Succeeded)
|
|
|
|
@ -52,14 +51,68 @@ namespace AuthStudy.WebApp.Controllers
|
|
|
|
|
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)
|
|
|
|
|
public IActionResult Login(string loginName, string loginPassword)
|
|
|
|
|
{
|
|
|
|
|
var info = new { Name = LoginName, Roles = "Admin" };
|
|
|
|
|
var info = new { Name = loginName, Roles = "Admin" };
|
|
|
|
|
|
|
|
|
|
return new JsonResult(info);
|
|
|
|
|
}
|
|
|
|
|