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 '

相关内容

热门资讯

aapoker猫腻!德州ai免... aapoker猫腻!德州ai免费,wpk微扑克真的有辅助,教你攻略(有挂总结)是一款可以让一直输的玩...
透视软件(WEpoke)外挂透... 透视软件(WEpoke)外挂透明挂辅助app(aapoker透明挂)切实真的有挂(详细透视可靠技巧)...
wpk辅助挂!wepoke里面... 1、wpk辅助挂!wepoke里面有ai吗,wpk微扑克模拟器,微扑克教程(有挂方法);代表性(透视...
透视科技(wePokE)透视辅... 透视科技(wePokE)透视辅助插件(wepoke一定有挂)果然有挂(详细透视普及教程)关于wepo...
智星德州菠萝有挂吗!德州wep... 一、智星德州菠萝有挂吗简介了解软件请加微:136704302智星德州菠萝有挂吗是一款在线扑克游戏平台...
透视黑科技(wepoKE)透视... 透视黑科技(wepoKE)透视辅助技巧(aapoker透明挂)本来真的是有挂(详细透视总结教程);小...
wepokeai代打!wepo... 自定义wepokeai代打系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用辅助器,...
透视游戏(wepokE)外挂透... 1、透视游戏(wepokE)外挂透明挂辅助软件(WePoKe外挂)都是真的有挂(详细透视扑克教程);...
aapoker透明挂!aapo... aapoker透明挂!aapoker俱乐部后天可以操作吗,微扑克系统发牌,我来教教你(有挂黑科技),...
透视游戏(WEPoke)透明挂... 1、透视游戏(WEPoke)透明挂辅助技巧(wepokeai代打的胜率)从前是真的有挂(详细透视必备...