Chinaunix首页 | 论坛 | 博客
  • 博客访问: 155822
  • 博文数量: 11
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 170
  • 用 户 组: 普通用户
  • 注册时间: 2020-07-22 17:26
个人简介

blender https://www.blender-3d.cn/ blender3D模型库

文章分类

全部博文(11)

文章存档

2020年(11)

我的朋友

分类: 信息化

2020-07-28 15:39:42

现在已经有越来越多的应用程式开始使用Azure AD的帐号登入验证机制了
若是在公司或是自己手边的应用程式要加入Azure AD的帐号验证,说实话是个颇麻烦的动作

本篇文章会一步一步说明怎么整合Azure AD帐号的验证到现有的MVC专案中,基本上照着步骤执行应该是都没有问题的

要整合Azure AD到现有的应用程式中,有两个大步骤要执行,第一个是必须先注册一个应用程式到公司的AAD里,第二则是在应用程式中加入一些程式码以完成登入的动作

在公司的AAD中注册应用程式,依照下面的步骤就可以完成

1.打开Azure Portal,并进入到公司的Azure AD项目中

2.在Azure AD的服务项目中,点选[应用程式注册]=>[新增注册]

3.在新增注册的页面中给予一个应用程式的名称,支援的帐户类型,如果要允许任何帐户都可以使用的话,可以选第二项,如果要连一般使用者或是个人帐号都可以使用,就选择第三项。而重新导向URI的部份,先选择[Web],并输入应用程式的实际网址。当然现在也可以先不给,之后再提供也可以。

4.按下注册后,先将下面框起来的[应用程式识别码]、[目录识别码]记下来,等一下会用到

5.接着进入[验证]的功能,然后按下[新增平台]=>[Web应用程式]的项目

在设定Web应用程式的页面中,设定[重新导向URI]的栏位,这个值会是在登入完成后导页到网站的网址,另外,[识别码权仗]也必须要打勾起来,不然无法取得登入者的资讯

6.最后,回到Azure AD的主画面中,把[主要网域]的网址复制下来,等一下也会用到

到这边,Azure AD上的设定就已经完成了,接下来要在现有的应用程式中加入Azure AD登入帐号的验证功能

1.在现有专案中的App_Start资料夹,加入[Startup.Auth.cs]的类别档

在档案中加入下面的内容

点击(此处)折叠或打开

  1. namespace MyWebApp
  2. {
  3.     using Microsoft.Owin.Security;
  4.     using Microsoft.Owin.Security.Cookies;
  5.     using Microsoft.Owin.Security.OpenIdConnect;
  6.     using Owin;

  7.     public partial class Startup
  8.     {
  9.         private static string clientId = ConfigurationManager.AppSettings["ida:ClientId"];
  10.         private static string aadInstance = EnsureTrailingSlash(ConfigurationManager.AppSettings["ida:AADInstance"]);
  11.         private static string tenantId = ConfigurationManager.AppSettings["ida:TenantId"];
  12.         private static string postLogoutRedirectUri = ConfigurationManager.AppSettings["ida:PostLogoutRedirectUri"];
  13.         private static string authority = aadInstance + tenantId;

  14.         public void ConfigureAuth(IAppBuilder app)
  15.         {
  16.             app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

  17.             app.UseCookieAuthentication(new CookieAuthenticationOptions());

  18.             app.UseOpenIdConnectAuthentication(
  19.                 new OpenIdConnectAuthenticationOptions
  20.                 {
  21.                     ClientId = clientId,
  22.                     Authority = authority,
  23.                     PostLogoutRedirectUri = postLogoutRedirectUri
  24.                 });
  25.         }

  26.         private static string EnsureTrailingSlash(string value)
  27.         {
  28.             if (value == null)
  29.             {
  30.                 value = string.Empty;
  31.             }

  32.             if (!value.EndsWith("/", StringComparison.Ordinal))
  33.             {
  34.                 return value + "/";
  35.             }

  36.             return value;
  37.         }
  38.     }
  39. }
2.在专案的根目录,加入[Startup.cs]

接着在档案中加入下面内容

点击(此处)折叠或打开

  1. namespace MyWebApp
  2. {
  3.     using Owin;
  4.     public partial class Startup
  5.     {
  6.         public void Configuration(IAppBuilder app)
  7.         {
  8.             ConfigureAuth(app);
  9.         }
  10.     }
  11. }
3.在Controller的目录下,新增[AccountController.cs]的MVC控制器

并在控制器中加入下面的程式码内容

点击(此处)折叠或打开

  1. namespace MyWebApp.Controllers
  2. {
  3.     using Microsoft.Owin.Security;
  4.     using Microsoft.Owin.Security.OpenIdConnect;
  5.     using Microsoft.Owin.Host.SystemWeb;
  6.     using Microsoft.Owin.Security.Cookies;

  7.     public class AccountController : Controller
  8.     {
  9.         public void SignIn()
  10.         {
  11.             // Send an OpenID Connect sign-in request.
  12.             if (!Request.IsAuthenticated)
  13.             {
  14.                 HttpContext.GetOwinContext().Authentication.Challenge(new AuthenticationProperties { RedirectUri = "/" },
  15.                     OpenIdConnectAuthenticationDefaults.AuthenticationType);
  16.             }
  17.         }

  18.         public void SignOut()
  19.         {
  20.             string callbackUrl = Url.Action("SignOutCallback", "Account", routeValues: null, protocol: Request.Url.Scheme);

  21.             HttpContext.GetOwinContext().Authentication.SignOut(
  22.                 new AuthenticationProperties { RedirectUri = callbackUrl },
  23.                 OpenIdConnectAuthenticationDefaults.AuthenticationType, CookieAuthenticationDefaults.AuthenticationType);
  24.         }

  25.         public ActionResult SignOutCallback()
  26.         {
  27.             if (Request.IsAuthenticated)
  28.             {
  29.                 // Redirect to home page if the user is authenticated.
  30.                 return RedirectToAction("Index", "Home");
  31.             }

  32.             return View();
  33.         }
  34.     }
  35. }
4.在画面的_Layout.cshtml中适当的位置,加入下面作为登入资讯的内容

点击(此处)折叠或打开

  1. @if (Request.IsAuthenticated)
  2. {
  3.     <text>
  4.         <ul class="nav navbar-nav navbar-right">
  5.             <li class="navbar-text">
  6.                 Hello, @User.Identity.
  7.             </li>
  8.             <li>
  9.                 @Html.ActionLink("Sign out", "SignOut", "Account")
  10.             </li>
  11.         </ul>
  12.     </text>
  13. }
  14. else
  15. {
  16.     <ul class="nav navbar-nav navbar-right">
  17.         <li>@Html.ActionLink("Sign in", "SignIn", "Account", routeValues: null, htmlAttributes: new { id = "loginLink" })</li>
  18.     </ul>
  19. }
5.最后,在web.config的档案中,加入下面的appSettings的内容,分别用前面在Azure AD的设定项目中的值,代入到设定内容里

点击(此处)折叠或打开

  1. <add key="ida:ClientId" value="[在這裡放入應用程式識別碼]" />
  2. <add key="ida:AADInstance" value="" />
  3. <add key="ida:Domain" value="[在這裡放入主要網域]" />
  4. <add key="ida:TenantId" value="[在這裡放入目錄識別碼]" />
  5. <add key="ida:PostLogoutRedirectUri" value="[登出時所使用的網址]" />
到这边,所有程式码要加入的内容也都完成了。其中有一些Nuget套件需要安装的,就请自行进行套件的安装,我就不再多作说明

程式码编译完成后,就可以实际将程式执行起来看一看结果

在画面的右上角,有出现刚刚加在_Layout.cshtml中的登入文字

点选登入文字后,跳至微软的登入画面

登入完成之后,会出现是否要授权这个应用程式存取帐号的权限,若是不授予的话就无法使用了,所以点选[接受]就可以
接着页面导回到应用程式的页面,可以看到应用程式已经抓到了登入者的帐号资讯,也可以点选登出的连结文字进行登出
Microsoft Azure AD的登入整合,提供了一个SaaS服务的平台多一种的帐号统一验证的机制,也可以在套用完成后快速的取得使用者资讯并加以应用,是个非常方便的帐号管理机制,也不用担心帐号密码存放在自己的资料库是否会有资安问题了
阅读(4742) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~