ASP.NET Core混合Razor Pages / React SPA应用程序,使用Cognito身份验证。
创始人
2024-09-15 21:00:55
0

要创建一个ASP.NET Core混合Razor Pages / React SPA应用程序,并使用Cognito身份验证,可以按照以下步骤进行操作:

  1. 创建一个新的ASP.NET Core Web应用程序项目。

  2. 安装必要的NuGet包。在项目文件中添加以下包引用:


  
  
  

  1. 在Startup.cs文件中进行必要的配置。添加以下代码:
using Amazon.Extensions.CognitoAuthentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;

public void ConfigureServices(IServiceCollection services)
{
    // 添加Cognito身份验证服务
    services.AddCognitoIdentity(config =>
    {
        config.UserPoolId = "YOUR_USER_POOL_ID";
        config.ClientId = "YOUR_CLIENT_ID";
    });

    // 配置身份验证中间件
    services.AddAuthentication(options =>
    {
        options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
    })
    .AddCookie()
    .AddOpenIdConnect(options =>
    {
        options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        options.ResponseType = "code";
        options.MetadataAddress = "https://cognito-idp.{region}.amazonaws.com/{userPoolId}/.well-known/openid-configuration";
        options.ClientId = "YOUR_CLIENT_ID";
        options.ClientSecret = "YOUR_CLIENT_SECRET";
    });

    // 添加Razor页面服务
    services.AddRazorPages();
}

请确保将YOUR_USER_POOL_IDYOUR_CLIENT_IDYOUR_CLIENT_SECRET替换为Cognito用户池和应用程序的实际值。

  1. 添加一个Razor页面来处理登录和注销逻辑。在Pages文件夹中创建一个名为Login.cshtml.cs的类,并添加以下代码:
using Amazon.AspNetCore.Identity.Cognito;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Threading.Tasks;

public class LoginModel : PageModel
{
    private readonly SignInManager _signInManager;

    public LoginModel(SignInManager signInManager)
    {
        _signInManager = signInManager;
    }

    public async Task OnPostAsync(string returnUrl = null)
    {
        returnUrl ??= Url.Content("~/");

        var result = await HttpContext.AuthenticateAsync(CookieAuthenticationDefaults.AuthenticationScheme);
        if (result?.Succeeded == true)
        {
            return LocalRedirect(returnUrl);
        }
        else
        {
            return Challenge(OpenIdConnectDefaults.AuthenticationScheme, new AuthenticationProperties { RedirectUri = returnUrl });
        }
    }

    public async Task OnGetLogoutAsync()
    {
        await _signInManager.SignOutAsync();
        return RedirectToPage("/Index");
    }
}
  1. 创建一个React SPA。在ClientApp文件夹中创建一个新的React应用程序。

  2. 在React应用程序中实现Cognito身份验证。可以使用AWS Amplify库来简化身份验证流程。在ClientApp文件夹中运行以下命令安装AWS Amplify:

npm install aws-amplify
  1. 在React应用程序的入口文件(通常是index.js)中添加以下代码来配置AWS Amplify:
import Amplify from 'aws-amplify';
import { AmazonCognitoIdentityProvider } from '@aws-amplify/auth';

Amplify.configure({
  Auth: {
    region: 'YOUR_REGION',
    userPoolId: 'YOUR_USER_POOL_ID',
    userPoolWebClientId: 'YOUR_CLIENT_ID',
    authenticationFlowType: 'USER_PASSWORD_AUTH',
    identityProvider: AmazonCognitoIdentityProvider
  }
});

确保将YOUR_REGIONYOUR_USER_POOL_IDYOUR_CLIENT_ID替换为Cognito用户池和应用程序的实际值。

  1. 在React应用程序中实现登录和注销逻辑。可以使用AWS Amplify提供的Auth模块来处理身份验证。以下是一个简单的示例:
import { useState } from '

相关内容

热门资讯

避免在粘贴双引号时向VS 20... 在粘贴双引号时向VS 2022添加反斜杠的问题通常是由于编辑器的自动转义功能引起的。为了避免这个问题...
安装apache-beam==... 出现此错误可能是因为用户的Python版本太低,而apache-beam==2.34.0需要更高的P...
Android Recycle... 要在Android RecyclerView中实现滑动卡片效果,可以按照以下步骤进行操作:首先,在项...
安装了Anaconda之后找不... 在安装Anaconda后,如果找不到Jupyter Notebook,可以尝试以下解决方法:检查环境...
omi系统和安卓系统哪个好,揭... OMI系统和安卓系统哪个好?这个问题就像是在问“苹果和橘子哪个更甜”,每个人都有自己的答案。今天,我...
原生ios和安卓系统,原生对比... 亲爱的读者们,你是否曾好奇过,为什么你的iPhone和安卓手机在操作体验上有着天壤之别?今天,就让我...
Android - 无法确定任... 这个错误通常发生在Android项目中,表示编译Debug版本的Java代码时出现了依赖关系问题。下...
Android - NDK 预... 在Android NDK的构建过程中,LOCAL_SRC_FILES只能包含一个项目。如果需要在ND...
Akka生成Actor问题 在Akka框架中,可以使用ActorSystem对象生成Actor。但是,当我们在Actor类中尝试...
Agora-RTC-React... 出现这个错误原因是因为在 React 组件中使用,import AgoraRTC from “ago...