Skip to content

Instantly share code, notes, and snippets.


matthijs110/ApplicationUser.cs Secret

Last active Oct 16, 2018
What would you like to do?
@using Microsoft.AspNetCore.Identity
@inject SignInManager<StudentUser> SignInManager
@inject UserManager<StudentUser> UserManager
@if (SignInManager.IsSignedIn(User))
<form asp-route="loguit" method="post" id="logoutForm" class="navbar-right">
<ul class="nav navbar-nav navbar-right">
<a asp-area="Identity" asp-page="/Account/Manage/Index" title="Manage">Hello @UserManager.GetUserName(User)!</a>
<button type="submit" class="btn btn-link navbar-btn navbar-link">Loguit</button>
public abstract class ApplicationUser : IdentityUser
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName => $"{FirstName} {LastName}";
public class EmployeeUser : ApplicationUser { }
public class Startup
#region Public Properties
/// <summary>
/// The application's configuration.
/// </summary>
public IConfiguration Configuration { get; }
/// <summary>
/// Information about the current environment.
/// </summary>
public IHostingEnvironment Environment { get; }
#region Private Properties
/// <summary>
/// The scoped logging service
/// </summary>
private ILogger logger;
#region Constructor
/// <summary>
/// Main entry point for start of web server
/// </summary>
/// <param name="configuration"></param>
/// <param name="environment"></param>
/// <param name="loggerFactory"></param>
public Startup(
IConfiguration configuration,
IHostingEnvironment environment,
ILoggerFactory loggerFactory
Configuration = configuration;
Environment = environment;
// Create a logger for the startup class
logger = loggerFactory.CreateLogger<Startup>();
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
// Change the cookie policy
services.Configure<CookiePolicyOptions>(options =>
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => false; // No cookie warning
options.MinimumSameSitePolicy = SameSiteMode.Lax;
options.Secure = CookieSecurePolicy.Always;
// Register the Application database context
services.AddDbContext<ApplicationDbContext>(options =>
// Use Microsoft SQL Server
// Grab the connection string from "appsettings.json"
// Add the identity service
services.AddIdentity<ApplicationUser, IdentityRole>()
// Adds UserStore and RoleStore from this context
// that is consumed by the UserManager and RoleManager
// Adds a provider that generates unique keys and hashes for things like
// forgot password link, change email etc...
// >> I tried this answer, but that didn't work unfortuantly (see answer below)
// Add MVC
// Keep using .NET Core 2.1
// Add the SignalR services
services.AddSignalR(options =>
// Only enable detailed errors when
// the application runs in a development environment
options.EnableDetailedErrors = Environment.IsDevelopment();
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
// When the application runs in a development environment...
if (env.IsDevelopment())
// Show detailed exception pages
// Show database errors
// Use a general error page
// Force the webserver to only accept HTTPS requests
// Redirect HTTP to HTTPS
// Serve static files from /wwwroot
// Add a cookie policy middleware
// Setup SignalR
app.UseSignalR(routes =>
// Enable user authentication
// Setup MVC routes
app.UseMvc(routes =>
// Default route pattern
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
public class StudentUser : ApplicationUser
public string StudentNumber { get; set; }
// A user belongs to one group
public Group Group { get; set; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment