本地ASP.NET开发页面使用AzureAD(AAD)验证登录

Azure和Office365已不是一个热门话题了,因为所谓的云时代已经走进了技术大师们的内心,大家多少有一定了解了,所以就不多介绍了,我的Blog中之前也写了很多关于Azure及Office365的相关文章,如果有兴趣的同学可以参考一下。一般企业内部如果使用了Azure或者Office365产品的话,都会跟本地的域进行验证绑定,然后可以使用相关工具(Dirsync & Azure AD Connect)将本地的域用户同步到AAD里面,然后配置ADFS使用本地的域用户格式进行登录及管理Azure相关服务等,当然也有其他的一些做法,具体就不介绍了,我们首先要知道,Azure 和Office365使用的是同一个AD服务,所以注册的时候自定义的域名称也就是唯一标识了,比如,当注册一个oa@ixmsoft.onmicrosoft.com(portal.office.com)的office365顶订阅的账户的时候,系统也会保留一个oa@imxmsoft.onmicrosoft.com;(manage.windowsazure.com)的订阅账户如果是国内供应商提高的需要将结尾更改为:partner.onm51CTO提醒您,请勿滥发广告!即可。在任何一个账户上新建账户都会显示。如果一开始注册了Office365,就可以通过https://manager.onmicrosoft.com进行提示注册或者管理对应的Azure订阅了,如果是注册了Azure,就可以通过访问https://portal.office.com 进行提示注册或管理对应的Office365订阅了。今天呢,我们主要介绍的是如果通过AAD验证本地开发的自定义web服务(ASP.NET)

首先是介绍一下大概的原理,原理就是我们需要在AAD中注册一个程序,然后通过配置程序值返回对应的URL信息,然后完成对应的验证,整个过程跟本地配置ADFS及ADFS验证登录过程非常类似,废话不多说了,具体见下:

我们当然是首先需要一个Azure账户,具体就不掩饰了,然后验证本地域;

我们首先加本地域添加到Azure中,我们只需要验证就可以,在dns中添加一条txt记录,不使用skype、exchange等服务即可;添加后,我们可以使用Azure AD Connect 工具将本地的用户同步到Azure AD中,当然,也可以直接在本地以添加的域新建用户。我们因为不用sso,所以不勾选sso

开始验证DNS

然后我们在公网DNS上添加验证记录

验证成功

然后我 可以看见在office365上也会出现域信息

接下来我们添加用户;然后后缀名以本地域进行添加

添加完成

接下来就是添加应用程序了

选择添加开的应用程序

我们定义任意名称

定义返回的url

添加完成后,我们也可以通过配置选项进行修改

我们首先选择客户端ID,复制数据

复制后,我们在代码中修改;我们首先是通过Visual Studio打开相关的代码项目;配置好对应的路劲

打开web.config配置文件

<?xml version="1.0" encoding="utf-8"?>    <!--       For more information on how to configure your ASP.NET application, please visit        http://go.microsoft.com/fwlink/?LinkId=301880        -->     <configuration>       <appSettings>         <add key="webpages:Version" value="3.0.0.0" />         <add key="webpages:Enabled" value="false" />         <add key="ClientValidationEnabled" value="true" />         <add key="UnobtrusiveJavascriptEnabled" value="true" />       <add key="ida:ClientId" value="cbc2336e-e7aa-445e-a751-8219830b873a" />     <!--在AAD中注册应用程序完成时获取到的ID-->         <add key="ida:Tenant" value="beyondsoft100.partner.onm51CTO提醒您,请勿滥发广告!" />       <!--contoso.onmicrosoft.com]--> <!--当前租户名称:当前目录中粗行间用户是所用的后缀名-->         <add key="ida:AADInstance" value=" https://login.chinacloudapi.cn/{0}"  />    <!--在AAD的端点好看也可以获取到-->         <add key="ida:PostLogoutRedirectUri" value=" https://localhost:44320?username=test/"  />     <!--在AAD中注册程序时所填写的返回URL地址-->            </appSettings>       <system.web>         <compilation debug="true" targetFramework="4.5" />         <httpRuntime targetFramework="4.5" />       </system.web>       <runtime>         <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">           <dependentAssembly>             <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />             <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />           </dependentAssembly>           <dependentAssembly>             <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />             <bindingRedirect oldVersion="0.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930" />           </dependentAssembly>           <dependentAssembly>             <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />             <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />           </dependentAssembly>           <dependentAssembly>             <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" culture="neutral" />             <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />           </dependentAssembly>           <dependentAssembly>             <assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f" culture="neutral" />             <bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2" />           </dependentAssembly>           <dependentAssembly>             <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />             <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />           </dependentAssembly>           <dependentAssembly>             <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />             <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />           </dependentAssembly>           <dependentAssembly>             <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />             <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />           </dependentAssembly>           <dependentAssembly>             <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />             <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />           </dependentAssembly>           <dependentAssembly>             <assemblyIdentity name="System.IdentityModel.Tokens.Jwt" publicKeyToken="31bf3856ad364e35" culture="neutral" />             <bindingRedirect oldVersion="0.0.0.0-4.0.2.28" newVersion="4.0.2.28" />           </dependentAssembly>           <dependentAssembly>             <assemblyIdentity name="Microsoft.IdentityModel.Protocol.Extensions" publicKeyToken="31bf3856ad364e35" culture="neutral" />             <bindingRedirect oldVersion="0.0.0.0-1.0.2.28" newVersion="1.0.2.28" />           </dependentAssembly>         </assemblyBinding>       </runtime>     </configuration>

通过对应的数据进行修改即可

所以修改为 fanwei.onmicrosoft.com

然后修改登录域名,国际版本为

国际版本为windows.net

最后,修改需要返回的url

,保存后,我们进行测试

我们可以查看已经开始执行debug模式

同时我们也可以查看到登录页面了,

我们单击登录

我们发现直接跳转到了我们的新建的WEBSERVER的应用程序界面了,

其实该界面也类似于我们的ADFS界面

我们使用user01@ixmsoft.com用户进行登录

其实已经登录成功了,提示第一次登录需要修改密码

我们按照提示修改一次密码,及正确登录

------------------------------------------------------------------------------------------------------------------

我们为了更好的显示,我们修改本地的显示页面

定义显示内容即可

登录成功了。页面的右上角正常显示当前登录的用户名