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 '

相关内容

热门资讯

记者揭秘!智星菠萝辅助(透视辅... 记者揭秘!智星菠萝辅助(透视辅助)拱趴大菠萝辅助神器,扑克教程(有挂细节);模式供您选择,了解更新找...
一分钟揭秘!约局吧能能开挂(透... 一分钟揭秘!约局吧能能开挂(透视辅助)hhpoker辅助靠谱,2024新版教程(有挂教学);约局吧能...
透视辅助!wepoker模拟器... 透视辅助!wepoker模拟器哪个好用(脚本)hhpoker辅助挂是真的,科技教程(有挂技巧);囊括...
透视代打!hhpkoer辅助器... 透视代打!hhpkoer辅助器视频(辅助挂)pokemmo脚本辅助,2024新版教程(有挂教程);风...
透视了解!约局吧德州真的有透视... 透视了解!约局吧德州真的有透视挂(透视脚本)德州局HHpoker透视脚本,必胜教程(有挂分析);亲,...
六分钟了解!wepoker挂底... 六分钟了解!wepoker挂底牌(透视)德普之星开辅助,详细教程(有挂解密);德普之星开辅助是一种具...
9分钟了解!wpk私人辅助(透... 9分钟了解!wpk私人辅助(透视)hhpoker德州透视,插件教程(有挂教学);风靡全球的特色经典游...
推荐一款!wepoker究竟有... 推荐一款!wepoker究竟有透视(脚本)哈糖大菠萝开挂,介绍教程(有挂技术);囊括全国各种wepo...
每日必备!wepoker有人用... 每日必备!wepoker有人用过(脚本)wpk有那种辅助,线上教程(有挂规律);wepoker有人用...
玩家必备教程!wejoker私... 玩家必备教程!wejoker私人辅助软件(脚本)哈糖大菠萝可以开挂,可靠技巧(有挂神器)申哈糖大菠萝...