ASP.NET Core混合Razor Pages / React SPA应用程序,使用Cognito身份验证。
创始人
2024-09-15 21:00:54
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 '

相关内容

热门资讯

关于!wepoker辅助透视软... 关于!wepoker辅助透视软件,大菠萝789辅助器下载,本来是有挂(有挂秘笈)一、wepoker辅...
分享一款!aapoker万能辅... 分享一款!aapoker万能辅助器,wepoker有插件吗,其实是有挂(的确有挂)1、在插件功能辅助...
新手必备!德普之星辅助工具如何... 新手必备!德普之星辅助工具如何设置,wepoker钻石怎么看底牌,原来是有挂(有挂分享)1、免费脚本...
来一盘!德州透视竞技联盟,po... 来一盘!德州透视竞技联盟,pokerworld辅助器,一直是有挂(有人有挂)1、来一盘!德州透视竞技...
指导大家!wejoker辅助脚... 指导大家!wejoker辅助脚本,wepokerplus透视脚本免费,原来真的是有挂(有挂攻略)1、...
科技分享!poker worl... 科技分享!poker world辅助,红龙poker辅助工具,都是真的是有挂(有挂细节)1、实时透视...
普及知识!wepoker如何设... 普及知识!wepoker如何设置透视,wepoker智能辅助插件,一直真的有挂(今日头条)1、该软件...
玩家必用!hhpoker辅助器... 玩家必用!hhpoker辅助器视频,wepoker怎么增加运气,果然是有挂(确实有挂)破解侠是真的助...
科技介绍!德普之星透视辅助插件... 科技介绍!德普之星透视辅助插件,wepoker私人局辅助挂,总是有挂(有挂秘笈)1、起透看视 辅助软...
攻略讲解!来玩app破解版,p... 攻略讲解!来玩app破解版,pokerworld辅助器,确实是有挂(有挂透视)1、起透看视 辅助软件...