mirror of
https://github.com/nsnail/IGeekFan.AspNetCore.Knife4jUI.git
synced 2025-08-04 13:38:01 +08:00
@ -0,0 +1,54 @@
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Authentication.Cookies;
|
||||
using IdentityServer4;
|
||||
using IdentityServer4.Test;
|
||||
|
||||
namespace OAuth2Integration.AuthServer.Controllers
|
||||
{
|
||||
[ApiExplorerSettings(IgnoreApi = true)]
|
||||
[Route("account")]
|
||||
public class AccountController : Controller
|
||||
{
|
||||
private readonly TestUserStore _userStore;
|
||||
|
||||
public AccountController()
|
||||
{
|
||||
_userStore = new TestUserStore(Config.TestUsers());
|
||||
}
|
||||
|
||||
[HttpGet("login")]
|
||||
public IActionResult Login(string returnUrl)
|
||||
{
|
||||
var viewModel = new LoginViewModel { Username = "joebloggs", Password = "pass123", ReturnUrl = returnUrl };
|
||||
|
||||
return View("/AuthServer/Views/Login.cshtml", viewModel);
|
||||
}
|
||||
|
||||
[HttpPost("login")]
|
||||
public async Task<IActionResult> Login([FromForm]LoginViewModel viewModel)
|
||||
{
|
||||
if (!_userStore.ValidateCredentials(viewModel.Username, viewModel.Password))
|
||||
{
|
||||
ModelState.AddModelError("", "Invalid username or password");
|
||||
viewModel.Password = string.Empty;
|
||||
return View("/AuthServer/Views/Login.cshtml", viewModel);
|
||||
}
|
||||
|
||||
// Use an IdentityServer-compatible ClaimsPrincipal
|
||||
var identityServerUser = new IdentityServerUser(viewModel.Username);
|
||||
identityServerUser.DisplayName = viewModel.Username;
|
||||
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, identityServerUser.CreatePrincipal());
|
||||
|
||||
return Redirect(viewModel.ReturnUrl);
|
||||
}
|
||||
}
|
||||
|
||||
public class LoginViewModel
|
||||
{
|
||||
public string ReturnUrl { get; set; }
|
||||
public string Username { get; set; }
|
||||
public string Password { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
using System.Threading.Tasks;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using IdentityServer4.Stores;
|
||||
using IdentityServer4.Services;
|
||||
using IdentityServer4.Models;
|
||||
|
||||
namespace OAuth2Integration.AuthServer.Controllers
|
||||
{
|
||||
[ApiExplorerSettings(IgnoreApi = true)]
|
||||
public class ConsentController : Controller
|
||||
{
|
||||
private readonly IIdentityServerInteractionService _interaction;
|
||||
private readonly IClientStore _clientStore;
|
||||
private readonly IResourceStore _resourceStore;
|
||||
|
||||
public ConsentController(
|
||||
IIdentityServerInteractionService interaction,
|
||||
IClientStore clientStore,
|
||||
IResourceStore resourceStore)
|
||||
{
|
||||
_interaction = interaction;
|
||||
_clientStore = clientStore;
|
||||
_resourceStore = resourceStore;
|
||||
}
|
||||
|
||||
[HttpGet("consent")]
|
||||
public async Task<IActionResult> Consent(string returnUrl)
|
||||
{
|
||||
var request = await _interaction.GetAuthorizationContextAsync(returnUrl);
|
||||
var client = await _clientStore.FindEnabledClientByIdAsync(request.ClientId);
|
||||
var resource = await _resourceStore.FindApiResourceAsync("api");
|
||||
|
||||
var viewModel = new ConsentViewModel
|
||||
{
|
||||
ReturnUrl = returnUrl,
|
||||
ClientName = client.ClientName,
|
||||
ScopesRequested = resource.Scopes.Where(s => request.ScopesRequested.Contains(s.Name))
|
||||
};
|
||||
|
||||
return View("/AuthServer/Views/Consent.cshtml", viewModel);
|
||||
}
|
||||
|
||||
[HttpPost("consent")]
|
||||
public async Task<IActionResult> Consent([FromForm]ConsentViewModel viewModel)
|
||||
{
|
||||
var request = await _interaction.GetAuthorizationContextAsync(viewModel.ReturnUrl);
|
||||
|
||||
// Communicate outcome of consent back to identityserver
|
||||
var consentResponse = new ConsentResponse
|
||||
{
|
||||
ScopesConsented = viewModel.ScopesConsented
|
||||
};
|
||||
await _interaction.GrantConsentAsync(request, consentResponse);
|
||||
|
||||
return Redirect(viewModel.ReturnUrl);
|
||||
}
|
||||
}
|
||||
|
||||
public class ConsentViewModel
|
||||
{
|
||||
public string ReturnUrl { get; set; }
|
||||
public string ClientName { get; set; }
|
||||
public IEnumerable<Scope> ScopesRequested { get; set; }
|
||||
public string[] ScopesConsented { get; set; }
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user