Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- *****************************************************************************************************
- * Login.aspx
- *****************************************************************************************************
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="OpenID_Test.Login" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title>Login</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:Button ID="btnLogin" runat="server" Text="Sign In" OnClick="btnLogin_Click" />
- </div>
- </form>
- </body>
- </html>
- *****************************************************************************************************
- * Login.aspx.cs
- *****************************************************************************************************
- using System;
- using System.Web;
- using Microsoft.Owin.Security;
- using Microsoft.Owin.Security.Cookies;
- using Microsoft.Owin.Security.OpenIdConnect;
- namespace OpenID_Test
- {
- public partial class Login : System.Web.UI.Page
- {
- // protected void Page_Load(object sender, EventArgs e)
- // {
- // }
- protected void btnLogin_Click(object sender, EventArgs e)
- {
- HttpContext.Current.GetOwinContext().Authentication.SignOut(
- CookieAuthenticationDefaults.AuthenticationType);
- HttpContext.Current.GetOwinContext().Authentication.Challenge(
- new AuthenticationProperties { RedirectUri = "/Home.aspx" },
- OpenIdConnectAuthenticationDefaults.AuthenticationType);
- }
- }
- }
- *****************************************************************************************************
- * Home.aspx
- *****************************************************************************************************
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Home.aspx.cs" Inherits="OpenID_Test.Home" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title>Home</title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <h1>Yo yo yo whatup</h1>
- </div>
- </form>
- </body>
- </html>
- *****************************************************************************************************
- * Home.aspx.cs
- *****************************************************************************************************
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- namespace OpenID_Test
- {
- public partial class Home : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- }
- }
- *****************************************************************************************************
- * Startup.cs
- *****************************************************************************************************
- using System.Threading.Tasks;
- using Microsoft.Owin;
- using Owin;
- using Microsoft.Owin.Diagnostics;
- using Microsoft.Owin.Security;
- using Microsoft.Owin.Security.Cookies;
- using Microsoft.Owin.Security.OpenIdConnect;
- using System.Configuration;
- [assembly: OwinStartup(typeof(OpenID_Test.Startup))]
- namespace OpenID_Test
- {
- public class Startup
- {
- public void Configuration(IAppBuilder app)
- {
- app.Use(async (context, next) =>
- {
- System.Diagnostics.Debug.WriteLine("Incoming request: " + context.Request.Method + " " + context.Request.Path + " Query: " + context.Request.QueryString);
- await next();
- System.Diagnostics.Debug.WriteLine("After first next(), status code: " + context.Response.StatusCode);
- });
- app.UseErrorPage(new ErrorPageOptions()
- {
- ShowCookies = true,
- ShowEnvironment = true,
- ShowQuery = true,
- ShowExceptionDetails = true,
- ShowHeaders = true,
- ShowSourceCode = true,
- SourceCodeLineCount = 10
- });
- System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
- app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
- app.UseCookieAuthentication(new CookieAuthenticationOptions());
- app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
- {
- ClientId = ConfigurationManager.AppSettings["ClientId"],
- ClientSecret = ConfigurationManager.AppSettings["ClientSecret"],
- Authority = ConfigurationManager.AppSettings["Authority"],
- RedirectUri = ConfigurationManager.AppSettings["RedirectUri"],
- CallbackPath = new PathString("/signin-oidc"),
- ResponseType = "code",
- Scope = "openid profile email",
- Notifications = new OpenIdConnectAuthenticationNotifications
- {
- SecurityTokenValidated = context =>
- {
- System.Diagnostics.Debug.WriteLine("********SecurityTokenValidated triggered********");
- context.AuthenticationTicket.Properties.RedirectUri = "/Home.aspx";
- return Task.FromResult(0);
- },
- AuthenticationFailed = context =>
- {
- System.Diagnostics.Debug.WriteLine("********AuthenticationFailed triggered********");
- return Task.FromResult(0);
- },
- AuthorizationCodeReceived = context =>
- {
- System.Diagnostics.Debug.WriteLine("********AuthorizationCodeReceived triggered********");
- return Task.FromResult(0);
- },
- MessageReceived = context =>
- {
- System.Diagnostics.Debug.WriteLine("********MessageReceived triggered********");
- return Task.FromResult(0);
- },
- RedirectToIdentityProvider = context =>
- {
- System.Diagnostics.Debug.WriteLine("********RedirectToIdentityProvider triggered********");
- return Task.FromResult(0);
- },
- SecurityTokenReceived = context =>
- {
- System.Diagnostics.Debug.WriteLine("********SecurityTokenReceived triggered********");
- return Task.FromResult(0);
- }
- }
- });
- app.Use(async (context, next) =>
- {
- System.Diagnostics.Debug.WriteLine("End of flow before next(): " + context.Request.Method + " " + context.Request.Path + " Query: " + context.Request.QueryString);
- await next();
- System.Diagnostics.Debug.WriteLine("End of flow after next(), status code: " + context.Response.StatusCode);
- });
- }
- }
- }
- *****************************************************************************************************
- * Web.config
- *****************************************************************************************************
- <?xml version="1.0" encoding="utf-8"?>
- <!--
- For more information on how to configure your ASP.NET application, please visit
- https://go.microsoft.com/fwlink/?LinkId=169433
- -->
- <configuration>
- <location path="signin-oidc">
- <system.webServer>
- <handlers>
- <!-- <clear /> -->
- <remove name="StaticFile" />
- <add name="OwinCallbackHandler"
- path="signin-oidc"
- verb="*"
- type="Microsoft.Owin.Host.SystemWeb.OwinHttpHandler, Microsoft.Owin.Host.SystemWeb"
- resourceType="Unspecified"
- preCondition="integratedMode,runtimeVersionv4.0" />
- </handlers>
- </system.webServer>
- </location>
- <appSettings>
- <add key="ClientId" value="<redacted>" />
- <add key="ClientSecret" value="<redacted>" />
- <add key="Authority" value="https://login.microsoftonline.com/<redacted>/v2.0" />
- <add key="RedirectUri" value="https://localhost:44368/signin-oidc" />
- </appSettings>
- <system.web>
- <compilation debug="true" targetFramework="4.8" />
- <httpRuntime targetFramework="4.8" />
- </system.web>
- <system.webServer>
- <modules runAllManagedModulesForAllRequests="true" />
- </system.webServer>
- <system.codedom>
- <compilers>
- <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
- <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+" />
- </compilers>
- </system.codedom>
- </configuration>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement