adds aspnet.identity

This commit is contained in:
Paul Schneider 2021-04-25 12:12:50 +01:00
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);

View file

@ -11,11 +11,11 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System.Linq;
using System.Threading.Tasks;
using IdentityServer4.Validation;
using System.Collections.Generic;
using System;
using IdentityServer4.Models;
namespace nuget_host.Models
using nuget_host.Models;
namespace nuget_host.Controllers
{
/// <summary>
/// This controller processes the consent UI

View file

@ -9,13 +9,14 @@ using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using nuget_host.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
namespace nuget_host.Models
namespace nuget_host.Controllers
{
[SecurityHeaders]
[AllowAnonymous]
@ -65,7 +66,7 @@ namespace nuget_host.Models
RedirectUri = Url.Action(nameof(Callback)),
Items =
{
{ "returnUrl", returnUrl },
{ "returnUrl", returnUrl },
{ "scheme", scheme },
}
};

View file

@ -4,9 +4,12 @@ using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using IdentityServer4.Services;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using nuget_host.Entities;
using nuget_host.Models;
namespace nuget_host.Controllers
@ -15,13 +18,20 @@ namespace nuget_host.Controllers
{
private readonly IIdentityServerInteractionService _interaction;
private readonly ILogger _logger;
IHostingEnvironment _environment;
readonly IHostingEnvironment _environment;
public IDataProtector DataProtector { get; }
public SmtpSettings Options { get; } //set only via Secret Manager
public HomeController(IIdentityServerInteractionService interaction, Microsoft.AspNetCore.Hosting.IHostingEnvironment environment, ILogger<HomeController> logger)
public HomeController(
IOptions<SmtpSettings> smtpSettings,
IDataProtectionProvider provider,
IIdentityServerInteractionService interaction, Microsoft.AspNetCore.Hosting.IHostingEnvironment environment, ILogger<HomeController> logger)
{
_interaction = interaction;
_environment = environment;
_logger = logger;
Options = smtpSettings.Value;
DataProtector = provider.CreateProtector(Options.ProtectionTitle);
}
public IActionResult Index()
@ -43,9 +53,9 @@ namespace nuget_host.Controllers
return View();
}
public IActionResult Test()
public IActionResult Privacy()
{
ViewData["Message"] = "Your test page.";
ViewData["Message"] = "Your Privacy page.";
return Ok(ViewData);
}

View file

@ -13,8 +13,8 @@ namespace nuget_host.Controllers
{
public class PackagesController : Controller
{
private ILogger<PackagesController> logger;
private IDataProtector protector;
private readonly ILogger<PackagesController> logger;
private readonly IDataProtector protector;
public PackagesController(ILoggerFactory loggerFactory, IDataProtectionProvider provider)
{