How to integrate cognito in ASP.NET WebForms App

0

I just start learn cognito and try to login my asp.net web app which I created with vs 2019. I can login with coginto and successfully return to my app. I check User.Identity.IsAuthenticated, is alway false. please tell me what is wrong. Thank you very much.

in Startup.Auth.cs, I add: app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType); app.UseCookieAuthentication(new CookieAuthenticationOptions()); app.UseOpenIdConnectAuthentication( new OpenIdConnectAuthenticationOptions { ClientId = "ignore", ClientSecret = " ignore", Authority = "https://cognito-idp.us-east-2.amazonaws.com/us-east-2_hc14JhCGv", RedirectUri = "https://localhost:44305/about.aspx",

                Scope="openid",
                ResponseType = "code",
                // Optionally include ClientSecret if needed (mostly for server-side apps)
                // ClientSecret = "your-client-secret",
                // Configure token validation parameters if you need to map claims
                TokenValidationParameters = new TokenValidationParameters
                {
                    NameClaimType = "name"
                },

            }) ;
1 Answer
0

It seems you're on the right track with integrating Amazon Cognito into your ASP.NET WebForms application, but there are a few adjustments you can make to ensure User.Identity.IsAuthenticated returns true after successful login.

  1. Make sure you're using the correct AuthenticationType. Instead of CookieAuthenticationDefaults.AuthenticationType, try using "Cookies" explicitly:

    app.SetDefaultSignInAsAuthenticationType("Cookies");

  2. In your OpenIdConnectAuthenticationOptions, add the following settings:

    ResponseType = "code id_token", Scope = "openid email profile", SaveTokens = true,

  3. Ensure that your Cognito User Pool is configured correctly with the right app client settings, including the correct callback URL.

  4. After the user is redirected back to your application, you may need to manually sign in the user. You can do this by adding a method to handle the OpenIdConnectAuthenticationNotification events:

    Notifications = new OpenIdConnectAuthenticationNotifications { AuthorizationCodeReceived = async n => { var identity = await n.AuthenticationTicket.GetIdentityAsync(); var props = new AuthenticationProperties(); n.OwinContext.Authentication.SignIn(props, identity); } }

  5. Make sure you're calling app.UseAuthentication() and app.UseAuthorization() in the correct order in your Startup.cs file.

If User.Identity.IsAuthenticated is still false after these changes, you may need to debug the authentication flow to ensure the identity is being properly set after the Cognito redirect. Also, check if you're accessing User.Identity.IsAuthenticated in the correct lifecycle method of your WebForms page, as it may not be available immediately after the redirect.
Sources
Amazon Cognito Identity Provider examples using SDK for .NET - AWS SDK Code Examples
Implement Role-based Access Control for .NET applications with Amazon Cognito | .NET on AWS Blog
How authentication works with Amazon Cognito - Amazon Cognito

profile picture
answered a month ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions