如何使用AWS Toolkit for Visual Studio

501 阅读8分钟

亚马逊网络服务是地球上最流行的云计算平台之一。你很有可能需要在AWS环境中工作,这意味着发布到他们的网络服务器服务之一,如Elastic Beanstalk。AWS平台是巨大的,对于开发者来说,这可能是一个令人生畏的任务。幸运的是,亚马逊已经发布了AWS Toolkit for Visual Studio 2013-2015和AWS Toolkit for Visual Studio 2017-2019,这使得你的网络应用发布到Elastic Beanstalk很容易。

在这篇文章中,你将使用Visual Studio 2019在.Net 5上构建一个ASP.Net MVC网络应用。你将使用Okta来保护这个应用程序。Okta利用他们的SSO提供商,可以很容易地创建一个安全的应用程序。一旦你的应用程序完成,你将使用AWS工具包将其发布到生产的Elastic Beanstalk实例。

编写您的MVC应用程序

打开Visual Studio 2019,选择创建一个新项目。在模板下选择ASP.Net Core Web Application,给你的应用程序起一个有意义的名字。我把我的命名为Okta_AWSToolkit ,我的代码中的命名空间将反映这一点。

接下来,按**"**创建",然后在 "创建新的ASP.NET Core网络应用程序"下选择ASP.NET Core网络应用程序(模型-视图-控制器),并确保选择ASP.NET Core 5.0。

等待片刻,让Visual Studio为该项目提供支架。

接下来,你将为该应用程序安装必要的依赖性。在这个例子中,你只需要NuGet的Okta AspNetCore包。你可以用包命令Install-Package Okta.AspNetCore -Version 3.5.0 ,或使用包管理器界面来安装。

你的应用程序应该有一个名为appsettings.json 的文件。你将使用它作为开发和生产中的appsettings文件的模板。在其中添加以下JSON:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "Okta": {
    "Domain": "",
    "ClientId": "",
    "ClientSecret": "",
    "CallbackPath": "/okta/callback"
  }
}

这个文件缺乏任何敏感信息,所以你可以在你的源代码控制中检查这个文件。你很快就会填入两个文件:appsettings.Development.jsonappsettings.Production.json 。现在,你可以添加这些文件,并添加与appsettings.json 相同的模板。

接下来,打开你的HomeController.cs 文件,添加以下代码:

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Okta_AWSTookit.Models;
using System.Diagnostics;

namespace Okta_AWSTookit.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;

        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }

        public IActionResult Index()
        {
            if (User.Identity.IsAuthenticated)
            {
                return RedirectToAction("Index", "Profile");
            }

            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}

这个控制器有一个Index 动作,将是你的主页。在Index 动作下,你要检查用户是否经过认证。如果他们是,那么你将把他们重定向到Profile 页面,否则,他们可以继续到主页。

接下来,创建一个名为AccountController.cs 的控制器,并添加以下代码:

using Microsoft.AspNetCore.Mvc;

using Microsoft.AspNetCore.Authentication.Cookies;
using Okta.AspNetCore;
using Microsoft.AspNetCore.Authentication;

namespace Okta_AWSTookit.Controllers
{
    public class AccountController : Controller
    {
        public IActionResult Login()
        {
            if (!HttpContext.User.Identity.IsAuthenticated)
            {
                return Challenge(OktaDefaults.MvcAuthenticationScheme);
            }

            return RedirectToAction("Index", "Home");
        }

        [HttpPost]
        public IActionResult Logout()
        {
            return new SignOutResult(
            new[]
            {
                OktaDefaults.MvcAuthenticationScheme,
                CookieAuthenticationDefaults.AuthenticationScheme
            },
            new AuthenticationProperties()
            {
                RedirectUri = "/Home/Index"
            });
        }
    }
}

这个控制器包含LoginLogout 动作。这里没有视图,因为Okta将为你提供登录页面。Login 动作应该在返回ChallengeResult 之前检查用户是否通过认证。Logout 动作将用户重定向到主页。

你需要的最后一个控制器是Profile 控制器。添加一个新的控制器,名为ProfileController.cs 。代码如下:

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Authorization;

namespace Okta_AWSTookit.Controllers
{
    public class ProfileController : Controller
    {
        private readonly ILogger<ProfileController> _logger;

        public ProfileController(ILogger<ProfileController> logger)
        {
            _logger = logger;
        }

        [Authorize]
        public IActionResult Index()
        {
            return View();
        }
    }
}

这个控制器的唯一目的是为Profile/Index 页面服务。Index 动作被装饰了Authorize 属性,这将自动把未认证的用户发送到Okta登录页面。

接下来,你可以开始处理你的视图了。首先要编辑的是Layout.cshtml 。将模板代码替换为以下内容:】

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>@ViewData["Title"] - Okta with Jenkins</title>
    <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
    <link rel="stylesheet" href="~/css/site.css" />
</head>
<body>
    <header>
        <nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
            <div class="container">
                <a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
                <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"
                        aria-expanded="false" aria-label="Toggle navigation">
                    <span class="navbar-toggler-icon"></span>
                </button>
                <div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
                    <ul class="navbar-nav justify-content-end flex-grow-1">
                        <li class="nav-item">
                            @if (User.Identity.IsAuthenticated)
                            {
                                <form asp-action="Logout" asp-controller="Account" asp-method="POST">

                                    <input type="submit" value="Logout" class="btn btn-secondary" />
                                </form>
                            }
                            else
                            {

                                <a class="btn btn-primary" asp-action="Login" asp-controller="Account">Login</a>
                            }
                        </li>
                    </ul>
                </div>
            </div>
        </nav>
    </header>
    <div class="container">
        <main role="main" class="pb-3">
            @RenderBody()
        </main>
    </div>

    <footer class="border-top footer text-muted">
        <div class="container">
            &copy; 2021 - Okta_AWSToolkit - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
        </div>
    </footer>
    <script src="~/lib/jquery/dist/jquery.min.js"></script>
    <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
    <script src="~/js/site.js" asp-append-version="true"></script>
    @await RenderSectionAsync("Scripts", required: false)
</body>
</html>

这段代码的大部分内容与模板相同,但有一个主要例外。你已经在导航条上添加了一个Login 按钮,如果用户通过了验证,这个按钮就会变成Logout

接下来,打开Home/Index.cshtml ,用下面的代码替换:

@{
    ViewData["Title"] = "Home Page";
}

<div class="text-center">
    <h1 class="display-4">Welcome</h1>
    <p>This is a brief tutorial on using <a href="https://aws.amazon.com/visualstudio/" target="_blank" rel="nofollow">the AWS Toolkit for Visual Studio 2019</a> with <a href="https://dotnet.microsoft.com/download/dotnet/5.0" target="_blank" rel="nofollow">.Net 5</a>, Secured with <a href="https://www.okta.com/" target="_blank" rel="nofollow">Okta</a></p>
    <p>Written by <a href="https://profile.fishbowlllc.com/" target="_blank" rel="nofollow">Nik Fisher</a></p>
</div>

这个页面提供了一些关于这个项目的额外信息,并取代了标准的ASP.NET介绍。

最后,为Profile/Index.cshtml 添加一个新的视图,并添加以下代码:

@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@

@{
    ViewData["Title"] = "Profile";
}

<div class="jumbotron">
    Welcome to your Profile
</div>

这里没有太多的事情,我们只是为用户呈现了一个页面,让他们明白他们已经通过了认证,并且已经进入了简介页面。你可以用任何你认为合适的代码来编辑这个页面。

最后,你需要编辑你的Startup.cs 代码来启用和配置Okta的认证。将该文件中的代码替换为以下内容:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

using Microsoft.AspNetCore.Authentication.Cookies;
using Okta.AspNetCore;

namespace Okta_AWSTookit
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = OktaDefaults.MvcAuthenticationScheme;
            })
           .AddCookie()
           .AddOktaMvc(new OktaMvcOptions
           {
                // Replace these values with your Okta configuration
               OktaDomain = Configuration.GetValue<string>("Okta:Domain"),
               ClientId = Configuration.GetValue<string>("Okta:ClientId"),
               ClientSecret = Configuration.GetValue<string>("Okta:ClientSecret"),
               CallbackPath = Configuration.GetValue<string>("Okta:CallbackPath")
           });

            services.AddControllersWithViews();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }
            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}

这个文件把所有的东西都集中在一起。你已经使用与你的ASPNETCORE_ENVIRONMENT 变量相匹配的appsettings 文件注册了Okta组件。现在你可以根据你的环境轻松改变你的Okta应用程序。

创建您的Okta开发应用程序

您需要为您的开发环境创建一个Okta应用程序。您可以使用Okta CLI工具。下载CLI工具并运行okta login 。这将引导您完成登录过程。一旦完成,运行okta apps create ,并按照屏幕上的提示操作。选择网络作为您的应用程序类型。对于你的重定向URI,使用你的应用程序提供的网络地址,并在它后面加上/okta/callback 。对于你的注销后重定向URI,使用相同的域名。这一次,在它后面加上/signout/callback 。如果你有多个授权服务器,你可以为这个应用程序选择默认。过了一会儿,CLI会把你的客户身份、客户秘密和发行人写到你工作的同一目录下的一个叫做.okta.env 的文件中。把这些值添加到你的appsettings.Development.json 文件中。

注意:你只需要你的Issuer的域名部分,而不是完整的URL。然后你可以删除.okta.env 的内容。

在这个时候,你可以开始你的项目,并确保一切都在开发模式下按照你的期望工作。

设置亚马逊工具箱

现在是时候把你的应用程序发布到你的Elastic Beanstalk上了。你应该已经安装了AWS工具包扩展,如果你没有,现在就去安装。第一次打开时,你会看到一个屏幕,上面有关于如何设置该扩展的说明。

该屏幕将指示你登录到AWS控制台的IAM用户页面并创建一个用户。点击该链接并设置一个新的用户。

创建新的用户,然后给用户附加一个管理员或权力用户策略。接下来,你将会看到访问密钥秘密密钥。将这些复制并粘贴到AWS工具箱中,然后按保存,并关闭。

发布到AWS Elastic Beanstalk

接下来,右击你的项目,点击发布到AWS Elastic Beanstalk。这将打开一个新的向导,指导你完成发布过程。

首先是应用程序标签。选择创建一个新的应用环境,然后选择下一步

应用环境界面下,选择你的环境的生产版本。这个动作会自动填入你的Elastic Beanstalk实例的URL。你要检查一下,以确保在继续前进之前,该URL是可用的。

选项标签下,你需要做的唯一改变是使用t2.micro实例来保持你的免费层状态。如果你以后需要扩大规模,亚马逊通过AWS工具包使之变得简单。

在选项标签下,你要选择发布配置。

最后,在视图选项卡上,点击部署,等待几分钟,网站就会被发布,服务器就会上线。当它准备好时,你会收到一个通知。从那里,你可以点击你的网站的链接,看到发布的版本。然而,在这个时候,你会注意到你的登录屏幕并不像预期那样工作。这是因为你仍然需要填充你的Okta生产凭证。

创建您的Okta生产应用程序

再次打开您的CLI,通过okta apps create 向导。但是这一次,不要使用Visual Studio提供的localhost 域,而是将重定向URI注销后重定向URI中的域替换为AWS发布应用程序的域。一会儿后,CLI将再次把你的详细信息写入.okta.env ,你可以用这些新的值替换你的appsettings.Production.json 。同样,在你的appsettings文件的域字段中只使用你的发行者的域。

你将需要最后一次将你的应用程序发布到AWS,以确保更新的appsettings.Production.json 到达服务器。一旦完成,你应该能够再次登录到你的应用程序。

总结

AWS工具包是一个强大的工具,可以帮助你从Visual Studio轻松地与AWS云服务集成。这篇文章是对该工具功能的简要介绍。如果你使用AWS进行开发,我建议你探索AWS Toolkit for Visual Studio的所有功能。