adds aspnet.identity

This commit is contained in:
Paul Schneider
2021-04-25 12:12:50 +01:00
parent a2f26f1e8e
commit dc37c9a9f0
26 changed files with 1106 additions and 122 deletions

View File

@ -13,12 +13,14 @@ using IdentityServer4.Test;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using nuget_host.Models;
using System;
using System.Linq;
using System.Threading.Tasks;
namespace nuget_host.Models
namespace nuget_host.Controllers
{
/// <summary>
/// This sample controller implements a typical login/logout/provision workflow for local and external accounts.
@ -29,27 +31,28 @@ namespace nuget_host.Models
[AllowAnonymous]
public class AccountController : Controller
{
private readonly TestUserStore _users;
private readonly IIdentityServerInteractionService _interaction;
private readonly IClientStore _clientStore;
private readonly IAuthenticationSchemeProvider _schemeProvider;
private readonly IEventService _events;
private readonly SignInManager<ApplicationUser> _signInManager;
private readonly UserManager<ApplicationUser> _userManager;
public AccountController(
IIdentityServerInteractionService interaction,
IClientStore clientStore,
IAuthenticationSchemeProvider schemeProvider,
IEventService events,
TestUserStore users = null)
SignInManager<ApplicationUser> signInManager,
UserManager<ApplicationUser> userManager)
{
// if the TestUserStore is not in DI, then we'll just use the global users collection
// this is where you would plug in your own custom identity management library (e.g. ASP.NET Identity)
_users = users ?? new TestUserStore(TestUsers.Users);
_interaction = interaction;
_clientStore = clientStore;
_schemeProvider = schemeProvider;
_events = events;
_signInManager = signInManager;
_userManager = userManager;
}
/// <summary>
@ -110,11 +113,13 @@ namespace nuget_host.Models
if (ModelState.IsValid)
{
// validate username/password against in-memory store
if (_users.ValidateCredentials(model.Username, model.Password))
// validate username/password
var user = await _userManager.FindByNameAsync(model.Username);
var signResult = await _signInManager.CheckPasswordSignInAsync(user, model.Password, true);
if (signResult.Succeeded)
{
var user = _users.FindByUsername(model.Username);
await _events.RaiseAsync(new UserLoginSuccessEvent(user.Username, user.SubjectId, user.Username, clientId: context?.ClientId));
await _events.RaiseAsync(new UserLoginSuccessEvent(user.UserName, user.Id, user.FullName, true, clientId: context?.ClientId));
// only set explicit expiration here if user chooses "remember me".
// otherwise we rely upon expiration configured in cookie middleware.
@ -129,9 +134,9 @@ namespace nuget_host.Models
};
// issue authentication cookie with subject ID and username
var isuser = new IdentityServerUser(user.SubjectId)
var isuser = new IdentityServerUser(user.Id)
{
DisplayName = user.Username
DisplayName = user.UserName
};
await HttpContext.SignInAsync(isuser, props);
@ -150,6 +155,8 @@ namespace nuget_host.Models
}
// request for a local page
await _signInManager.SignInAsync(user, model.RememberLogin && AccountOptions.AllowRememberLogin);
if (Url.IsLocalUrl(model.ReturnUrl))
{
return Redirect(model.ReturnUrl);