Skip to content

Instantly share code, notes, and snippets.

@iwillspeak
Last active March 4, 2019 09:13
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save iwillspeak/1e2d78f36c89a898891148c47befdf4b to your computer and use it in GitHub Desktop.
Save iwillspeak/1e2d78f36c89a898891148c47befdf4b to your computer and use it in GitHub Desktop.
Http Auth in ASP .NET Core
using Microsoft.AspNetCore.Builder;
namespace test.Middleware
{
public static class MiddlewareExtensions
{
public static IApplicationBuilder UseSimpleTokenAuth(this IApplicationBuilder app)
{
return app.UseMiddleware<SimpleTokenAuthMiddleware>();
}
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
namespace test
{
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
}
}
using System.Security.Principal;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
namespace test.Middleware
{
public class SimpleTokenAuthMiddleware
{
private readonly RequestDelegate _next;
public SimpleTokenAuthMiddleware(RequestDelegate next)
{
_next = next;
}
public Task Invoke(HttpContext context)
{
if (context.Request.Query.ContainsKey("auth"))
{
var identity = new GenericIdentity("test_user", "queryString");
context.User.AddIdentity(identity);
}
return _next(context);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace test
{
using System.Text;
using Middleware;
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app
.UseSimpleTokenAuth()
.Run(async context =>
{
if (context.User?.Identities == null)
{
await context.Response.WriteAsync("No user identities");
}
foreach (var identity in context.User.Identities)
{
var sb = new StringBuilder();
sb.AppendLine("Identity");
sb.AppendLine($" Name: {identity.Name}");
sb.AppendLine($" Label: {identity.Label}");
sb.AppendLine($" AuthType: {identity.AuthenticationType}");
sb.AppendLine($" Authenticated?: {identity.IsAuthenticated}");
var claims = string.Join(", ", identity.Claims.Select(c => c.Value));
sb.AppendLine($" Claims: {claims}");
await context.Response.WriteAsync(sb.ToString());
}
});
}
}
}
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.2" />
</ItemGroup>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment