Advertisement
Guest User

Untitled

a guest
Apr 10th, 2025
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.73 KB | Source Code | 0 0
  1. *****************************************************************************************************
  2. * Login.aspx
  3. *****************************************************************************************************
  4.  
  5. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="OpenID_Test.Login" %>
  6.  
  7. <!DOCTYPE html>
  8.  
  9. <html xmlns="http://www.w3.org/1999/xhtml">
  10. <head runat="server">
  11. <title>Login</title>
  12. </head>
  13. <body>
  14. <form id="form1" runat="server">
  15. <div>
  16. <asp:Button ID="btnLogin" runat="server" Text="Sign In" OnClick="btnLogin_Click" />
  17. </div>
  18. </form>
  19. </body>
  20. </html>
  21.  
  22. *****************************************************************************************************
  23. * Login.aspx.cs
  24. *****************************************************************************************************
  25.  
  26. using System;
  27. using System.Web;
  28. using Microsoft.Owin.Security;
  29. using Microsoft.Owin.Security.Cookies;
  30. using Microsoft.Owin.Security.OpenIdConnect;
  31.  
  32.  
  33. namespace OpenID_Test
  34. {
  35. public partial class Login : System.Web.UI.Page
  36. {
  37. // protected void Page_Load(object sender, EventArgs e)
  38. // {
  39.  
  40. // }
  41.  
  42. protected void btnLogin_Click(object sender, EventArgs e)
  43. {
  44. HttpContext.Current.GetOwinContext().Authentication.SignOut(
  45. CookieAuthenticationDefaults.AuthenticationType);
  46. HttpContext.Current.GetOwinContext().Authentication.Challenge(
  47. new AuthenticationProperties { RedirectUri = "/Home.aspx" },
  48. OpenIdConnectAuthenticationDefaults.AuthenticationType);
  49. }
  50. }
  51. }
  52.  
  53. *****************************************************************************************************
  54. * Home.aspx
  55. *****************************************************************************************************
  56.  
  57. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Home.aspx.cs" Inherits="OpenID_Test.Home" %>
  58.  
  59. <!DOCTYPE html>
  60.  
  61. <html xmlns="http://www.w3.org/1999/xhtml">
  62. <head runat="server">
  63. <title>Home</title>
  64. </head>
  65. <body>
  66. <form id="form1" runat="server">
  67. <div>
  68. <h1>Yo yo yo whatup</h1>
  69. </div>
  70. </form>
  71. </body>
  72. </html>
  73.  
  74. *****************************************************************************************************
  75. * Home.aspx.cs
  76. *****************************************************************************************************
  77.  
  78. using System;
  79. using System.Collections.Generic;
  80. using System.Linq;
  81. using System.Web;
  82. using System.Web.UI;
  83. using System.Web.UI.WebControls;
  84.  
  85. namespace OpenID_Test
  86. {
  87. public partial class Home : System.Web.UI.Page
  88. {
  89. protected void Page_Load(object sender, EventArgs e)
  90. {
  91.  
  92. }
  93. }
  94. }
  95.  
  96. *****************************************************************************************************
  97. * Startup.cs
  98. *****************************************************************************************************
  99.  
  100. using System.Threading.Tasks;
  101. using Microsoft.Owin;
  102. using Owin;
  103. using Microsoft.Owin.Diagnostics;
  104. using Microsoft.Owin.Security;
  105. using Microsoft.Owin.Security.Cookies;
  106. using Microsoft.Owin.Security.OpenIdConnect;
  107. using System.Configuration;
  108.  
  109. [assembly: OwinStartup(typeof(OpenID_Test.Startup))]
  110. namespace OpenID_Test
  111. {
  112. public class Startup
  113. {
  114. public void Configuration(IAppBuilder app)
  115. {
  116. app.Use(async (context, next) =>
  117. {
  118. System.Diagnostics.Debug.WriteLine("Incoming request: " + context.Request.Method + " " + context.Request.Path + " Query: " + context.Request.QueryString);
  119. await next();
  120. System.Diagnostics.Debug.WriteLine("After first next(), status code: " + context.Response.StatusCode);
  121. });
  122. app.UseErrorPage(new ErrorPageOptions()
  123. {
  124. ShowCookies = true,
  125. ShowEnvironment = true,
  126. ShowQuery = true,
  127. ShowExceptionDetails = true,
  128. ShowHeaders = true,
  129. ShowSourceCode = true,
  130. SourceCodeLineCount = 10
  131. });
  132.  
  133. System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
  134. app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
  135. app.UseCookieAuthentication(new CookieAuthenticationOptions());
  136. app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
  137. {
  138. ClientId = ConfigurationManager.AppSettings["ClientId"],
  139. ClientSecret = ConfigurationManager.AppSettings["ClientSecret"],
  140. Authority = ConfigurationManager.AppSettings["Authority"],
  141. RedirectUri = ConfigurationManager.AppSettings["RedirectUri"],
  142. CallbackPath = new PathString("/signin-oidc"),
  143. ResponseType = "code",
  144. Scope = "openid profile email",
  145. Notifications = new OpenIdConnectAuthenticationNotifications
  146. {
  147. SecurityTokenValidated = context =>
  148. {
  149. System.Diagnostics.Debug.WriteLine("********SecurityTokenValidated triggered********");
  150. context.AuthenticationTicket.Properties.RedirectUri = "/Home.aspx";
  151. return Task.FromResult(0);
  152. },
  153. AuthenticationFailed = context =>
  154. {
  155. System.Diagnostics.Debug.WriteLine("********AuthenticationFailed triggered********");
  156. return Task.FromResult(0);
  157. },
  158. AuthorizationCodeReceived = context =>
  159. {
  160. System.Diagnostics.Debug.WriteLine("********AuthorizationCodeReceived triggered********");
  161. return Task.FromResult(0);
  162. },
  163. MessageReceived = context =>
  164. {
  165. System.Diagnostics.Debug.WriteLine("********MessageReceived triggered********");
  166. return Task.FromResult(0);
  167. },
  168. RedirectToIdentityProvider = context =>
  169. {
  170. System.Diagnostics.Debug.WriteLine("********RedirectToIdentityProvider triggered********");
  171. return Task.FromResult(0);
  172. },
  173. SecurityTokenReceived = context =>
  174. {
  175. System.Diagnostics.Debug.WriteLine("********SecurityTokenReceived triggered********");
  176. return Task.FromResult(0);
  177. }
  178. }
  179. });
  180. app.Use(async (context, next) =>
  181. {
  182. System.Diagnostics.Debug.WriteLine("End of flow before next(): " + context.Request.Method + " " + context.Request.Path + " Query: " + context.Request.QueryString);
  183. await next();
  184. System.Diagnostics.Debug.WriteLine("End of flow after next(), status code: " + context.Response.StatusCode);
  185. });
  186. }
  187. }
  188. }
  189.  
  190. *****************************************************************************************************
  191. * Web.config
  192. *****************************************************************************************************
  193.  
  194. <?xml version="1.0" encoding="utf-8"?>
  195. <!--
  196. For more information on how to configure your ASP.NET application, please visit
  197. https://go.microsoft.com/fwlink/?LinkId=169433
  198. -->
  199. <configuration>
  200. <location path="signin-oidc">
  201. <system.webServer>
  202. <handlers>
  203. <!-- <clear /> -->
  204. <remove name="StaticFile" />
  205. <add name="OwinCallbackHandler"
  206. path="signin-oidc"
  207. verb="*"
  208. type="Microsoft.Owin.Host.SystemWeb.OwinHttpHandler, Microsoft.Owin.Host.SystemWeb"
  209. resourceType="Unspecified"
  210. preCondition="integratedMode,runtimeVersionv4.0" />
  211. </handlers>
  212. </system.webServer>
  213. </location>
  214. <appSettings>
  215. <add key="ClientId" value="<redacted>" />
  216. <add key="ClientSecret" value="<redacted>" />
  217. <add key="Authority" value="https://login.microsoftonline.com/<redacted>/v2.0" />
  218. <add key="RedirectUri" value="https://localhost:44368/signin-oidc" />
  219. </appSettings>
  220. <system.web>
  221. <compilation debug="true" targetFramework="4.8" />
  222. <httpRuntime targetFramework="4.8" />
  223. </system.web>
  224. <system.webServer>
  225. <modules runAllManagedModulesForAllRequests="true" />
  226. </system.webServer>
  227. <system.codedom>
  228. <compilers>
  229. <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" />
  230. <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=\&quot;Web\&quot; /optionInfer+" />
  231. </compilers>
  232. </system.codedom>
  233. </configuration>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement