现在已经有越来越多的应用程式开始使用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]的类别档
在档案中加入下面的内容
-
namespace MyWebApp
-
{
-
using Microsoft.Owin.Security;
-
using Microsoft.Owin.Security.Cookies;
-
using Microsoft.Owin.Security.OpenIdConnect;
-
using Owin;
-
-
public partial class Startup
-
{
-
private static string clientId = ConfigurationManager.AppSettings["ida:ClientId"];
-
private static string aadInstance = EnsureTrailingSlash(ConfigurationManager.AppSettings["ida:AADInstance"]);
-
private static string tenantId = ConfigurationManager.AppSettings["ida:TenantId"];
-
private static string postLogoutRedirectUri = ConfigurationManager.AppSettings["ida:PostLogoutRedirectUri"];
-
private static string authority = aadInstance + tenantId;
-
-
public void ConfigureAuth(IAppBuilder app)
-
{
-
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
-
-
app.UseCookieAuthentication(new CookieAuthenticationOptions());
-
-
app.UseOpenIdConnectAuthentication(
-
new OpenIdConnectAuthenticationOptions
-
{
-
ClientId = clientId,
-
Authority = authority,
-
PostLogoutRedirectUri = postLogoutRedirectUri
-
});
-
}
-
-
private static string EnsureTrailingSlash(string value)
-
{
-
if (value == null)
-
{
-
value = string.Empty;
-
}
-
-
if (!value.EndsWith("/", StringComparison.Ordinal))
-
{
-
return value + "/";
-
}
-
-
return value;
-
}
-
}
-
}
2.在专案的根目录,加入[Startup.cs]
接着在档案中加入下面内容
-
namespace MyWebApp
-
{
-
using Owin;
-
public partial class Startup
-
{
-
public void Configuration(IAppBuilder app)
-
{
-
ConfigureAuth(app);
-
}
-
}
-
}
3.在Controller的目录下,新增[AccountController.cs]的MVC控制器
并在控制器中加入下面的程式码内容
-
namespace MyWebApp.Controllers
-
{
-
using Microsoft.Owin.Security;
-
using Microsoft.Owin.Security.OpenIdConnect;
-
using Microsoft.Owin.Host.SystemWeb;
-
using Microsoft.Owin.Security.Cookies;
-
-
public class AccountController : Controller
-
{
-
public void SignIn()
-
{
-
// Send an OpenID Connect sign-in request.
-
if (!Request.IsAuthenticated)
-
{
-
HttpContext.GetOwinContext().Authentication.Challenge(new AuthenticationProperties { RedirectUri = "/" },
-
OpenIdConnectAuthenticationDefaults.AuthenticationType);
-
}
-
}
-
-
public void SignOut()
-
{
-
string callbackUrl = Url.Action("SignOutCallback", "Account", routeValues: null, protocol: Request.Url.Scheme);
-
-
HttpContext.GetOwinContext().Authentication.SignOut(
-
new AuthenticationProperties { RedirectUri = callbackUrl },
-
OpenIdConnectAuthenticationDefaults.AuthenticationType, CookieAuthenticationDefaults.AuthenticationType);
-
}
-
-
public ActionResult SignOutCallback()
-
{
-
if (Request.IsAuthenticated)
-
{
-
// Redirect to home page if the user is authenticated.
-
return RedirectToAction("Index", "Home");
-
}
-
-
return View();
-
}
-
}
-
}
4.在画面的_Layout.cshtml中适当的位置,加入下面作为登入资讯的内容
-
@if (Request.IsAuthenticated)
-
{
-
<text>
-
<ul class="nav navbar-nav navbar-right">
-
<li class="navbar-text">
-
Hello, @User.Identity.
-
</li>
-
<li>
-
@Html.ActionLink("Sign out", "SignOut", "Account")
-
</li>
-
</ul>
-
</text>
-
}
-
else
-
{
-
<ul class="nav navbar-nav navbar-right">
-
<li>@Html.ActionLink("Sign in", "SignIn", "Account", routeValues: null, htmlAttributes: new { id = "loginLink" })</li>
-
</ul>
-
}
5.最后,在web.config的档案中,加入下面的appSettings的内容,分别用前面在Azure AD的设定项目中的值,代入到设定内容里
-
<add key="ida:ClientId" value="[在這裡放入應用程式識別碼]" />
-
<add key="ida:AADInstance" value="" />
-
<add key="ida:Domain" value="[在這裡放入主要網域]" />
-
<add key="ida:TenantId" value="[在這裡放入目錄識別碼]" />
-
<add key="ida:PostLogoutRedirectUri" value="[登出時所使用的網址]" />
到这边,所有程式码要加入的内容也都完成了。其中有一些Nuget套件需要安装的,就请自行进行套件的安装,我就不再多作说明
程式码编译完成后,就可以实际将程式执行起来看一看结果
在画面的右上角,有出现刚刚加在_Layout.cshtml中的登入文字
点选登入文字后,跳至微软的登入画面
登入完成之后,会出现是否要授权这个应用程式存取帐号的权限,若是不授予的话就无法使用了,所以点选[接受]就可以
接着页面导回到应用程式的页面,可以看到应用程式已经抓到了登入者的帐号资讯,也可以点选登出的连结文字进行登出
Microsoft Azure AD的登入整合,提供了一个SaaS服务的平台多一种的帐号统一验证的机制,也可以在套用完成后快速的取得使用者资讯并加以应用,是个非常方便的帐号管理机制,也不用担心帐号密码存放在自己的资料库是否会有资安问题了
阅读(4742) | 评论(0) | 转发(1) |