Hi All, my Project is developed in Asp.net (webform). By using OWIN i need to implement below 2 requirement
1. I want to integrate IDP (Azure AD) User Authentication on that project like when the user try to visit our website then first the user will be redirected to IDP login page (Azure AD). There the user will enter his credential and then after successful authentication the user will be redirected to our project home page.
2. Once the user redirected then in our code we should able to capture the access_token and that access_token we need to pass in the header while calling webservice for loading other functionalities.
For now I am able to achieve 1st requirement but facing issue in implementing 2nd requirement i.e capturing the Access_token on my project after successful authentication in IDP.
So can anybody please help me on this by providing a working example.

Abhishek ChadhaRank #402 · 6.2K reputationPosted May 28, 2024, 1:21 AM EDT
Here's how you can capture the access token in your ASP.NET WebForms application:
2. Configure OWIN middleware in your
Startup.csfile:using Microsoft.Owin; using Microsoft.Owin.Security; using Microsoft.Owin.Security.Cookies; using Microsoft.Owin.Security.OpenIdConnect; using Owin; [assembly: OwinStartup(typeof(YourNamespace.Startup))] namespace YourNamespace { public class Startup { public void Configuration(IAppBuilder app) { app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType); app.UseCookieAuthentication(new CookieAuthenticationOptions()); app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions { ClientId = "your-client-id", Authority = "https://login.microsoftonline.com/your-tenant-id", RedirectUri = "https://yourwebsite.com/signin-oidc", ResponseType = "id_token", Scope = "openid", TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters { ValidateIssuer = false // You may want to change this depending on your requirements }, Notifications = new OpenIdConnectAuthenticationNotifications { SecurityTokenValidated = context => { // Here you can capture the access token var accessToken = context.ProtocolMessage.AccessToken; // You can store it in session, database, etc. return Task.FromResult(0); }, RedirectToIdentityProvider = context => { // This is where you can add additional parameters if (context.ProtocolMessage.RequestType == OpenIdConnectRequestType.Authentication) { // Add parameters here if needed } return Task.FromResult(0); } } }); } } }3. In your code-behind file or wherever you need to use the access token, you can retrieve it from the session or wherever you stored it:
protected void Page_Load(object sender, EventArgs e) { var accessToken = Session["AccessToken"] as string; // Use the access token as needed }sunil mohanty109 reputationPosted May 28, 2024, 10:56 AM EDT
Hi Abhishek, Thank you for your response. I have used the below code provided by you but still I am getting accessToken as null. Infact all the properties coming under context.ProtocolMessage are null.
Tuhin PaulRank #7 · 71.9K reputationPosted May 30, 2024, 2:56 PM EDT
There are several reasons why you might be getting a null access token and null properties in
context.ProtocolMessageeven after following the provided code. Check you've replaced "your-client-id" and "your-tenant-id" with the actual values from your Azure Active Directory (AAD) application registration.