Skip to content

Instantly share code, notes, and snippets.

@matthijs110
Last active October 16, 2018 08:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthijs110/94a55df9d4df35f2fb6596b54dedff25 to your computer and use it in GitHub Desktop.
Save matthijs110/94a55df9d4df35f2fb6596b54dedff25 to your computer and use it in GitHub Desktop.
@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">
<li>
<a asp-area="Identity" asp-page="/Account/Manage/Index" title="Manage">Hello @UserManager.GetUserName(User)!</a>
</li>
<li>
<button type="submit" class="btn btn-link navbar-btn navbar-link">Loguit</button>
</li>
</ul>
</form>
}
public abstract class ApplicationUser : IdentityUser
{
[PersonalData]
public string FirstName { get; set; }
[PersonalData]
public string LastName { get; set; }
[NotMapped]
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; }
#endregion
#region Private Properties
/// <summary>
/// The scoped logging service
/// </summary>
private ILogger logger;
#endregion
#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>();
}
#endregion
// 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
options.UseSqlServer(
// Grab the connection string from "appsettings.json"
Configuration.GetConnectionString("DefaultConnection")));
// Add the identity service
services.AddIdentity<ApplicationUser, IdentityRole>()
// Adds UserStore and RoleStore from this context
// that is consumed by the UserManager and RoleManager
.AddEntityFrameworkStores<ApplicationDbContext>()
// Adds a provider that generates unique keys and hashes for things like
// forgot password link, change email etc...
.AddDefaultTokenProviders();
// >> I tried this answer, but that didn't work unfortuantly (see answer below)
services.AddScoped<UserManager<StudentUser>>();
services.AddScoped<UserManager<EmployeeUser>>();
// Add MVC
services.AddMvc()
// Keep using .NET Core 2.1
.SetCompatibilityVersion(CompatibilityVersion.Version_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
app.UseDeveloperExceptionPage();
// Show database errors
app.UseDatabaseErrorPage();
}
else
{
// Use a general error page
app.UseExceptionHandler("/Home/Error");
// Force the webserver to only accept HTTPS requests
app.UseHsts();
}
// Redirect HTTP to HTTPS
app.UseHttpsRedirection();
// Serve static files from /wwwroot
app.UseStaticFiles();
// Add a cookie policy middleware
app.UseCookiePolicy();
// Setup SignalR
app.UseSignalR(routes =>
{
routes.MapHub<ClassroomHub>("/hubs/classroom");
});
// Enable user authentication
app.UseAuthentication();
// Setup MVC routes
app.UseMvc(routes =>
{
// Default route pattern
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
public class StudentUser : ApplicationUser
{
[PersonalData]
[Required]
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