Created
March 20, 2013 21:55
-
-
Save loudej/5208875 to your computer and use it in GitHub Desktop.
auth sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ 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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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