Skip to content

Instantly share code, notes, and snippets.

@loudej
Created March 20, 2013 21:55
Show Gist options
  • Save loudej/5208875 to your computer and use it in GitHub Desktop.
Save loudej/5208875 to your computer and use it in GitHub Desktop.
auth sample
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Katana.Experimental.WebApp.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
Your name is <%=User.Identity.Name %>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Security.Principal;
using System.Threading.Tasks;
using Katana.Experimental.WebApp;
namespace Katana.Experimental.WebApp
{
using AppFunc = Func<IDictionary<string, object>, Task>;
using AuthenticateFunc = Func<IDictionary<string, object>, Task<string>>;
public class FakeAuthMiddleware
{
private readonly AppFunc _next;
private readonly AuthenticateFunc _authenticate;
public FakeAuthMiddleware(AppFunc next, AuthenticateFunc authenticate)
{
_next = next;
_authenticate = authenticate;
}
public async Task Invoke(IDictionary<string, object> env)
{
var userName = await _authenticate(env);
if (userName != null)
{
env["server.User"] = new GenericPrincipal(new GenericIdentity(userName, "FakeAuth"), new string[0]);
}
await _next(env);
}
}
}
namespace Owin
{
using AuthenticateFunc = Func<IDictionary<string, object>, Task<string>>;
public static class FakeAuthExtensions
{
public static IAppBuilder UseFakeAuth(this IAppBuilder app, string userName)
{
return UseFakeAuth(app, async env => userName);
}
public static IAppBuilder UseFakeAuth(this IAppBuilder app, AuthenticateFunc authenticate)
{
app.Use(typeof(FakeAuthMiddleware), authenticate);
var stageMarker = StageMarker(app);
if (stageMarker != null)
{
stageMarker(app, "Authenticate");
}
return app;
}
static Action<IAppBuilder, string> StageMarker(IAppBuilder app)
{
object value;
return app.Properties.TryGetValue("integratedpipeline.StageMarker", out value) ? value as Action<IAppBuilder, string> : null;
}
}
}
using System;
using System.Web;
using System.Web.Routing;
namespace Katana.Experimental.WebApp
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.Add(new Route("routetable", new SampleRouteHandler()));
}
}
public class SampleRouteHandler : IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
return new SampleHttpHandler();
}
}
public class SampleHttpHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Write("Hello, " + context.User.Identity.Name + "!");
}
public bool IsReusable
{
get { return true; }
}
}
}
using Owin;
namespace Katana.Experimental.WebApp
{
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
app.MapPath("/test1", map => map.UseTestPage());
app.UseFakeAuth(async env => "Bob");
app.MapPath("/test2", map => map.UseTestPage());
}
}
}
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<system.webServer>
<modules>
<add name="owin" type="Microsoft.Owin.Host.SystemWeb.OwinHttpModule, Microsoft.Owin.Host.SystemWeb" />
</modules>
</system.webServer>
</configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment