首页 > 解决方案 > .NET Core Web 应用程序的 Cognito 身份验证

问题描述

我对使用 AWS Cognito 有点陌生,但我在这里找到了一个非常方便的教程,用于将用户身份验证到 .NET Core Web 应用程序

https://catalog.us-east-1.prod.workshops.aws/v2/workshops/02696107-09ac-4313-a6cb-3798048b07d7/en-US/9-how-to-authenticate-users-in-my-应用

但是....我的要求之一是使用自定义登录屏幕,而不是托管的。

由于这个非常有用的页面,我尝试使用 AWS Amplify JS 库并取得了一些成功(即我可以连接它并进行身份验证):

https://arcoirislabs.medium.com/aws-amplify-in-vanilla-javascript-6c4d8b3f29b0

问题是我在尝试合并这两个解决方案时遇到问题。我相信托管方法(第一个链接)设置 cookie 以启用身份验证,因为它使用以下内容:

            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
            })
            .AddCookie()
            .AddOpenIdConnect(options =>
            {
                options.ResponseType = Configuration["Authentication:Cognito:ResponseType"];
                options.MetadataAddress = Configuration["Authentication:Cognito:MetadataAddress"];
                options.ClientId = Configuration["Authentication:Cognito:ClientId"];
                options.SaveTokens = true;

            });

我想弄清楚的是如何使用 Amplify 库来设置正确的 cookie,以便上述授权配置与我的 .NET 项目正常工作。

以下是调用 Amplify 库进行登录的相关代码:

  function signIn() {
    var email = document.getElementById("email").value;
    var pass = document.getElementById("pwd").value;
    try {
      const user = aws_amplify.Amplify.Auth.signIn(
        email, pass
      );
      console.log(user);
      alert("Signed In. Check Console")
    } catch (error) {
      console.log('error signing in', error);
      alert("Error signing in. Check Console")
    }
  }

我认为这应该很简单,但不幸的是,我简单的大脑在这里没有看到解决方案。如果有人能提供答案,我将永远感激不尽。

谢谢

蒂姆

标签: authenticationamazon-cognitoamplify

解决方案


推荐阅读