<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Shahid Shaikh on Medium]]></title>
        <description><![CDATA[Stories by Shahid Shaikh on Medium]]></description>
        <link>https://medium.com/@msshaikh-simform?source=rss-a83b8533939a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*dmbNkD5D-u45r44go_cf0g.png</url>
            <title>Stories by Shahid Shaikh on Medium</title>
            <link>https://medium.com/@msshaikh-simform?source=rss-a83b8533939a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 05:09:06 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@msshaikh-simform/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Secure User Authentication in .NET Using AWS Cognito]]></title>
            <link>https://medium.com/simform-engineering/secure-user-authentication-in-net-using-aws-cognito-dfbdfbe0acdd?source=rss-a83b8533939a------2</link>
            <guid isPermaLink="false">https://medium.com/p/dfbdfbe0acdd</guid>
            <category><![CDATA[api]]></category>
            <category><![CDATA[net-9]]></category>
            <category><![CDATA[cognito]]></category>
            <category><![CDATA[aws-cognito]]></category>
            <category><![CDATA[cognito-user-pools]]></category>
            <dc:creator><![CDATA[Shahid Shaikh]]></dc:creator>
            <pubDate>Fri, 06 Jun 2025 09:45:43 GMT</pubDate>
            <atom:updated>2025-06-06T09:45:43.423Z</atom:updated>
            <content:encoded><![CDATA[<h3>Secure User Authentication in .NET Using AWS Cognito</h3><h4><em>Integrate AWS Cognito with .NET for seamless user sign-up, login, and secure identity management.</em></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*p4fChHil6IlpxQJzQTlwpA.png" /></figure><p>In the modern era of web and mobile applications, ensuring secure and efficient user authentication is more important than ever. Whether you’re building a customer-facing platform or an internal enterprise tool, managing user identities can quickly become complex if not handled properly. This is where <strong>Amazon Cognito</strong> comes into play.</p><p><strong>AWS Cognito</strong> is a cloud-based authentication service provided by Amazon Web Services that takes care of the heavy lifting involved in user identity management.</p><p>In this blog post, we’ll walk through how to integrate AWS Cognitowith a <strong>.NET Web API</strong> application. We’ll cover the configuration of a AWS CognitoUser Pool, how to set up the backend to interact with it, and how to handle basic authentication flows such as sign-up and sign-in. By the end of this tutorial, you’ll have a foundational setup ready for building secure applications using AWS Cognito and .NET.</p><h3>Features &amp; Advantages</h3><p>When building authentication and identity management in .NET applications, AWS Cognito offers a comprehensive set of features designed to reduce development effort while enhancing security and scalability.</p><p><strong>User Pools</strong></p><p>Cognito User Pools act as a secure user directory that handles all aspects of user lifecycle — such as registration, login, email/phone verification, and account recovery. In .NET, you can interact with User Pools using the AWS SDK (AWSSDK.CognitoIdentityProvider) to implement sign-up and login functionalities directly in your Web API.</p><p><strong>Federated Identities</strong></p><p>Cognito enables integration with external identity providers like Google, Facebook, Apple, and enterprise SAML providers. This makes it easy to offer single sign-on (SSO) in .NET apps. You can configure identity federation in the AWS console and use Cognito tokens to authenticate users in your backend APIs.</p><p><strong>Multi-Factor Authentication (MFA)</strong></p><p>To enhance security, Cognito supports both SMS-based and TOTP (Time-Based One-Time Password) MFA. In your .NET backend, you can trigger MFA challenges and validate user responses using the Cognito API, adding an extra layer of protection to sensitive workflows.</p><p><strong>Hosted UI</strong></p><p>For rapid development or applications without custom UI requirements, Cognito offers a built-in, customizable Hosted UI for login and registration. This can be easily integrated into .NET frontends or SPAs (like Angular or React) that connect with your .NET backend. You can also handle redirects and tokens seamlessly using standard OAuth 2.0 protocols.</p><p><strong>Token-Based Authentication</strong></p><p>Cognito issues <strong>JWT tokens</strong> (ID token, access token, and refresh token) upon successful authentication. These tokens can be passed from the client to your .NET Web API to authorize access to protected resources. In .NET, you can use middleware to validate these tokens and extract user claims securely.</p><p><strong>User Management APIs</strong></p><p>Cognito provides rich API support for managing users programmatically. Using the AWS SDK in .NET, you can:</p><ul><li>Register users</li><li>Confirm email/phone numbers</li><li>Resend confirmation codes</li><li>Reset passwords</li><li>Enable/disable user accounts</li><li>Retrieve user details</li></ul><p>This enables full control over the user lifecycle without manual intervention.</p><h3>Use Cases</h3><p>Enterprise employee login systems</p><p>E-commerce customer authentication</p><p>Mobile app user sign-up/sign-in</p><p>Integration with third-party identity providers (Google, Apple, Facebook)</p><h3>Let’s get started</h3><h4>Prerequisites</h4><p>To use this POC, you’ll need the following:</p><ul><li><strong>AWS Account</strong>: Set up AWS Cognito in your AWS account.</li><li><strong>.NET Backend</strong>: The backend manages authentication and securely stores credentials.</li></ul><h3>Setting up your AWS Cognito</h3><p>Follow these steps to set up AWS Cognito:</p><p><strong>1. Create a Cognito User Pool</strong></p><ol><li>Navigate to the <strong>Amazon Cognito</strong> service in the AWS Management Console.</li><li>Click <strong>Create a user pool</strong>.</li><li>Provide a name for the user pool and choose <strong>Review defaults</strong> or customize settings as needed.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*LMoBsmXLEtf_kaKjpckQPA.png" /><figcaption>Configuring User Pool</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*JEg-ejhTtSN8PoKeqCod7A.png" /><figcaption>Configuring Password policy</figcaption></figure><p><strong>2. Create an App Client</strong></p><ol><li>In your Cognito User Pool, navigate to the <strong>App clients</strong> section.</li><li>Click <strong>Add an app client</strong> and configure the settings under login page:</li></ol><ul><li>Disable <strong>Generate client secret</strong> (since this is a public client).</li><li>Add <strong>callback URL(s)</strong>: The URL users will be redirected to after authentication (e.g., <a href="http://localhost:4200/callback).">http://localhost:4200/callback).</a></li><li>Add <strong>sign-out URL(s)</strong>: Where users will be redirected after logging out (e.g., <a href="http://localhost:4200/).">http://localhost:4200/).</a></li><li>Enable <strong>OAuth 2.0 grant types</strong>: Select <strong>Authorization code grant</strong> for secure token exchange.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*ZiB1YOjQnNu2VZrUDVZpFg.png" /><figcaption>Creating App Client</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*wh-OZbrY0ZnLDKVnj8LroA.png" /><figcaption>Viewing App Client information</figcaption></figure><p><strong>3. Configure OAuth Scopes</strong></p><p>In the <strong>App client settings</strong>, choose the following <strong>OpenID Connect (OIDC) scopes</strong>:</p><ul><li><strong>Email</strong> (to retrieve the user’s email).</li><li><strong>OpenID</strong> (to retrieve basic user info).</li><li><strong>Profile</strong> (optional, if you need extra profile details).</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*DVoaeQZkfnebGqEHq31h3g.png" /><figcaption>Selecting OpenID Connect scopes</figcaption></figure><p><strong>4. Configure the Managed Login Pages</strong></p><ol><li>Go to the <strong>App client settings</strong> in Cognito.</li><li>Under <strong>Login page</strong>,</li><li>Add <strong>callback URLs</strong> (e.g., <a href="http://localhost:4200/callback).">http://localhost:4200/callback).</a></li><li>Add <strong>sign-out URLs</strong> (e.g., <a href="http://localhost:4200/).">http://localhost:4200/).</a></li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*Ss5Ck2IyBDK5Qjly94mzDg.png" /><figcaption>Configuring Managed Login — Setting up URLs</figcaption></figure><p><strong>5. Secure Your Application</strong></p><ul><li>Store the <strong>Cognito User Pool ID</strong>, <strong>App Client ID</strong>, and <strong>Client Secret</strong> securely in your <strong>backend</strong>.</li><li>The frontend (Angular app) handles only the user interface and interacts with the backend to initiate authentication and receive tokens.</li></ul><h3>Setting up your .Net Core project</h3><h4>Necessary Packages</h4><p>To integrate AWS Cognito with your .NET backend, install the following packages:</p><pre>Install-Package Microsoft.AspNetCore.Authentication.JwtBearer<br>Install-Package AWSSDK.CognitoIdentityProvider<br>Install-Package Amazon.Extensions.CognitoAuthentication<br>Install-Package Amazon.AspNetCore.Identity.Cognito</pre><p><strong>appsettings.json</strong></p><pre>  &quot;Cognito&quot;: {<br>    &quot;UserPoolId&quot;: &quot;&quot;,<br>    &quot;ClientId&quot;: &quot;&quot;,<br>    &quot;Domain&quot;: &quot;&quot;,<br>    &quot;Region&quot;: &quot;,<br>    &quot;RedirectUri&quot;: &quot;http://localhost:4200/callback&quot;,<br>    &quot;LogoutUri&quot;: &quot;http://localhost:4200&quot;<br>  },<br>  &quot;Google&quot;: {<br>    &quot;ClientId&quot;: &quot;&quot;,<br>    &quot;ClientSecret&quot;: &quot;&quot;<br>  },</pre><p><strong>Program.cs Code for .NET Authentication</strong></p><p>Configure the JWT Bearer Authentication in the Program.cs of your .NET backend:</p><pre>using Microsoft.AspNetCore.Authentication.JwtBearer;<br>using Amazon.CognitoIdentityProvider;<br>using Amazon.Extensions.CognitoAuthentication;<br>using Amazon.AspNetCore.Identity.Cognito;<br>using Microsoft.IdentityModel.Tokens;<br>using Amazon;<br><br>var builder = WebApplication.CreateBuilder(args);<br><br>// Load AWS Configuration<br>var cognitoUserPoolId = builder.Configuration[&quot;Cognito:UserPoolId&quot;];<br>var cognitoClientId = builder.Configuration[&quot;Cognito:ClientId&quot;];<br>var cognitoRegion = builder.Configuration[&quot;Cognito:Region&quot;];<br><br>var awsRegion = RegionEndpoint.GetBySystemName(cognitoRegion);<br><br>// Register AWS Cognito Identity Provider<br>builder.Services.AddSingleton&lt;IAmazonCognitoIdentityProvider&gt;(sp =&gt;<br>{<br>    var config = new AmazonCognitoIdentityProviderConfig { RegionEndpoint = awsRegion };<br>    return new AmazonCognitoIdentityProviderClient(config);<br>});<br><br>// Add Authentication with JWT<br>builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)<br>    .AddJwtBearer(options =&gt;<br>    {<br>        options.Authority = $&quot;https://cognito-idp.{cognitoRegion}.amazonaws.com/{cognitoUserPoolId}&quot;;<br>        options.TokenValidationParameters = new TokenValidationParameters<br>        {<br>            ValidateIssuerSigningKey = true,<br>            ValidateIssuer = true,<br>            ValidateAudience = true,<br>            ValidIssuer = $&quot;https://cognito-idp.{cognitoRegion}.amazonaws.com/{cognitoUserPoolId}&quot;,<br>            ValidAudience = cognitoClientId,<br>            NameClaimType = &quot;http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress&quot;<br>        };<br>    });<br><br>var app = builder.Build();</pre><h4>Other configurations for your .NET application</h4><p>Creating Cognito service: CognitoAuthService</p><pre>using Amazon.CognitoIdentityProvider;<br>using Amazon.CognitoIdentityProvider.Model;<br>using Microsoft.Extensions.Configuration;<br>using CognitoAuth_Models;<br><br>namespace CognitoAuth_Services;<br><br>   public class CognitoAuthService : ICognitoAuthService<br>   {<br>       private readonly IAmazonCognitoIdentityProvider _provider;<br>       private readonly IConfiguration _config;<br>       private readonly string _clientId;<br><br>       public CognitoAuthService(IAmazonCognitoIdentityProvider provider, IConfiguration config)<br>       {<br>           _provider = provider;<br>           _config = config;<br>           _clientId = _config[&quot;Cognito:ClientId&quot;];<br>       }<br><br>       // Register new user<br>       public async Task&lt;AuthResponse&gt; SignUpAsync(SignUpModel model)<br>       {<br>           var request = new SignUpRequest<br>           {<br>               ClientId = _clientId,<br>               Username = model.Email,<br>               Password = model.Password,<br>               UserAttributes = new List&lt;AttributeType&gt;<br>           {<br>               new AttributeType { Name = &quot;email&quot;, Value = model.Email }<br>           }<br>           };<br><br>           try<br>           {<br>               await _provider.SignUpAsync(request);<br>               return new AuthResponse { Success = true, Message = &quot;User registered successfully.&quot; };<br>           }<br>           catch (Exception ex)<br>           {<br>               return new AuthResponse { Success = false, Message = ex.Message };<br>           }<br>       }<br><br>       // Confirm user registration using the code sent to email<br>       public async Task&lt;AuthResponse&gt; ConfirmUserAsync(string email, string code)<br>       {<br>           var request = new ConfirmSignUpRequest<br>           {<br>               ClientId = _clientId,<br>               Username = email,<br>               ConfirmationCode = code<br>           };<br><br>           try<br>           {<br>               await _provider.ConfirmSignUpAsync(request);<br>               return new AuthResponse { Success = true, Message = &quot;User confirmed successfully.&quot; };<br>           }<br>           catch (Exception ex)<br>           {<br>               return new AuthResponse { Success = false, Message = ex.Message };<br>           }<br>       }<br><br>       // Login using email and password<br>       public async Task&lt;AuthResponse&gt; LoginAsync(LoginModel model)<br>       {<br>           var authParams = new Dictionary&lt;string, string&gt;<br>       {<br>           { &quot;USERNAME&quot;, model.Email },<br>           { &quot;PASSWORD&quot;, model.Password }<br>       };<br><br>           var request = new InitiateAuthRequest<br>           {<br>               AuthFlow = AuthFlowType.USER_PASSWORD_AUTH,<br>               ClientId = _clientId,<br>               AuthParameters = authParams<br>           };<br><br>           try<br>           {<br>               var response = await _provider.InitiateAuthAsync(request);<br>               return new AuthResponse<br>               {<br>                   Success = true,<br>                   Token = response.AuthenticationResult?.IdToken,<br>                   AccessToken = response.AuthenticationResult?.AccessToken,<br>                   Message = &quot;Login successful.&quot;<br>               };<br>           }<br>           catch (Exception ex)<br>           {<br>               return new AuthResponse { Success = false, Message = ex.Message };<br>           }<br>       }<br><br>       // Forgot password<br>       public async Task&lt;AuthResponse&gt; ForgotPasswordAsync(string email)<br>       {<br>           try<br>           {<br>               var request = new ForgotPasswordRequest<br>               {<br>                   Username = email,<br>                   ClientId = _clientId<br>               };<br><br>               var response = await _cognitoProvider.ForgotPasswordAsync(request);<br>               return new AuthResponse { Success = true, Message = &quot;Verification code sent.&quot; };<br>           }<br>           catch (Exception ex)<br>           {<br>               return new AuthResponse { Success = false, Message = ex.Message };<br>           }<br>       }<br><br>       // Confirm Forgot password<br>       public async Task&lt;AuthResponse&gt; ConfirmForgotPasswordAsync(ResetPasswordModel model)<br>       {<br>           try<br>           {<br>               var request = new ConfirmForgotPasswordRequest<br>               {<br>                   ClientId = _clientId,<br>                   Username = model.Email,<br>                   ConfirmationCode = model.Code,<br>                   Password = model.NewPassword<br>               };<br><br>               var response = await _cognitoProvider.ConfirmForgotPasswordAsync(request);<br>               return new AuthResponse { Success = true, Message = &quot;Password reset successful.&quot; };<br>           }<br>           catch (InvalidPasswordException)<br>           {<br>               return new AuthResponse<br>               {<br>                   Success = false,<br>                   Message = &quot;Password must be at least 8 characters long and include at least one uppercase letter, one lowercase letter, one number, and one special character.&quot;<br>               };<br>           }<br>           catch (Exception ex)<br>           {<br>               return new AuthResponse { Success = false, Message = ex.Message };<br>           }<br>       }<br>   }</pre><p>Make sure to configure the <strong>callback URLs</strong> and <strong>sign-out URLs</strong> properly, and store your credentials securely on the backend. The frontend will handle the user interface and authentication flow.</p><h3>Conclusion</h3><p>Integrating AWS Cognito with a .NET Web API provides a secure, scalable, and easy-to-manage solution for handling user authentication. Whether you’re building a web app, mobile app, or internal tool, AWS Cognitotakes care of common identity features like sign-up, login, password recovery, and social login — so you can focus more on your application and less on security plumbing.</p><p>With just a few configurations and some code, you can quickly add authentication to your .NET project while following industry best practices. It’s a powerful tool that grows with your application’s needs.</p><p>For a hands-on reference, you can explore the Git repository <a href="https://github.com/dotnet-simformsolutions/AWS-cognito">here</a>.</p><blockquote><strong><em>For more updates on the latest tools and technologies, follow the </em></strong><a href="https://medium.com/simform-engineering"><strong><em>Simform Engineering</em></strong></a><strong><em> blog.</em></strong></blockquote><blockquote><strong><em>Follow Us: </em></strong><a href="https://twitter.com/simform"><strong><em>Twitter</em></strong></a><strong><em> | </em></strong><a href="https://www.linkedin.com/company/simform/"><strong><em>LinkedIn</em></strong></a></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dfbdfbe0acdd" width="1" height="1" alt=""><hr><p><a href="https://medium.com/simform-engineering/secure-user-authentication-in-net-using-aws-cognito-dfbdfbe0acdd">Secure User Authentication in .NET Using AWS Cognito</a> was originally published in <a href="https://medium.com/simform-engineering">Simform Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>