zoukankan      html  css  js  c++  java
  • Migrating an ASP.NET MVC application to ADFS authentication

    I recently built an ASP.NET application at work to help track internal use of our products. It's been really well received, but only a few days after launch one of our managers came over and asked if we could move the site to Azure so that people didn't need to be in the office or on the VPN. Getting sites published on Azure itself is fairly easy with the publishing tools in Visual Studio - but dealing with authentication itself is a bit more difficult. The site uses Windows authentication - not something suitable for use on Azure.

    There seem to be a few options when migrating away:

    * Windows Azure Active Directory (effectively replicate your AD into Azure)
    * Azure Access Control Services (now deprecated)
    * On premise ADFS (can be made public for authentication outside the office)

    Given ACS is deprecated and we already had an ADFS server I went down the ADFS route. It's not as easy as it should be - you can't change the authentication option easily in VS 2013 after you've created a project. Here's how I did it:

    (Throughout the following, replace with the hostname of your application and with the hostname of your ADFS server)

    Open your web.config file

    Add the following to (or create if necessary) the configSections element:

    <configSections>
    <section name="system.identityModel" type="System.IdentityModel.Configuration.SystemIdentityModelSection, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
    <section name="system.identityModel.services" type="System.IdentityModel.Services.Configuration.SystemIdentityModelServicesSection, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
    </configSections>

    Add the following to (or create if necessary) the appSettings element:

    <appSettings>
    <add key="ida:FederationMetadataLocation" value="https://<sts.local>/federationmetadata/2007-06/federationmetadata.xml" />
    <add key="ida:Realm" value="https://<app.local>/" />
    <add key="ida:AudienceUri" value="https://<app.local>/" />
    </appSettings>

    Change the authentication mode to None:

    <authentication mode="None" />

    Add the following configuration sections:

    <system.webServer>
    <modules>
    <add name="WSFederationAuthenticationModule" type="System.IdentityModel.Services.WSFederationAuthenticationModule, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" preCondition="managedHandler" />
    <add name="SessionAuthenticationModule" type="System.IdentityModel.Services.SessionAuthenticationModule, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" preCondition="managedHandler" />
    </modules>
    </system.webServer>
    <system.identityModel>
    <identityConfiguration>
    <audienceUris>
    <add value="https://<app.local>/" />
    </audienceUris>
    <securityTokenHandlers>
    <add type="System.IdentityModel.Services.Tokens.MachineKeySessionSecurityTokenHandler, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    <remove type="System.IdentityModel.Tokens.SessionSecurityTokenHandler, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    </securityTokenHandlers>
    <certificateValidation certificateValidationMode="None" />
    <issuerNameRegistry type="System.IdentityModel.Tokens.ValidatingIssuerNameRegistry, System.IdentityModel.Tokens.ValidatingIssuerNameRegistry">
    <authority name="http://<sts.local>/adfs/services/trust">
    <keys>
    <add thumbprint="<thumbprint>" />
    </keys>
    <validIssuers>
    <add name="http://<sts.local>/adfs/services/trust" />
    </validIssuers>
    </authority>
    </issuerNameRegistry>
    </identityConfiguration>
    </system.identityModel>
    <system.identityModel.services>
    <federationConfiguration>
    <cookieHandler requireSsl="true" />
    <wsFederation passiveRedirectEnabled="true" issuer="https://<sts.local>/adfs/ls/" realm="https://<app.local>/" requireHttps="true" />
    </federationConfiguration>
    </system.identityModel.services>

    Add the following references

    System.IdentityModel
    System.IdentityModel.Services
    System.IdentityModel.Tokens.ValidatingIssuer

    You now need to register your app with the ADFS server as a "relying party"

  • 相关阅读:
    JQuery 判断某个属性是否存在 hasAttr
    微信支付开发-Senparc.Weixin.MP详解
    c# 两个数组比较,将重复部分去掉,返回不重复部分
    String.Format数字格式化输出 {0:N2} {0:D2} {0:C2
    asp.net 时间比较,常用于在某段时间进行操作
    关于C#正则表达式MatchCollection类的总结,正则表达式的应用
    开发错误11:Configuration with name ‘default’ not found
    Android新旧版本Notification
    Okio 1.9简单入门
    Android  PNG透明图片转JPG格式背景变黑
  • 原文地址:https://www.cnblogs.com/kiracn/p/4564655.html
Copyright © 2011-2022 走看看