asp.net-mvc-4 - 如何使用商业身份提供者(例如 ADFS2)

标签 asp.net-mvc-4 ws-federation

我运行的是 Win 7、IIS 7.0、VS2012
我已经创建了 asp.mvc4 网络应用程序
我在单独的 VM 上安装了 ADFS2.0

在 VS 2012 中使用身份和访问工具

我选择使用业务身份提供程序(例如 ADFS2)并键入 STS 元数据文档的 URL。

https://server.local/federationmetadata/2007-06/federationmetadata.xml

编辑了网络配置

<system.web>
...
<httpModules>
...
    <remove name="FormsAuthentication" />
</httpModules>
</system.web>

和这个
<system.webServer>
...
   <modules>
   ...
      <remove name="FormsAuthentication" />
   </modules>
</system.webServer>

还检查了该项目是否禁用了 Windows 身份验证

该网站重定向到这样的网址 http://localhost/WebSite/login.aspx?ReturnUrl=%2fWebSite%2f,其中出现“找不到资源”错误。

我还需要摆弄什么才能完成这项工作?

Microsoft doco 轻量级 http://blogs.msdn.com/b/vbertocci/archive/2012/03/15/windows-identity-foundation-tools-for-visual-studio-11-part-iii-connecting-with-a-business-sts-e-g-adfs2.aspx

我已经在本地开发 STS MS Identity and Access Tool MVC 4 上遇到过类似的问题

最佳答案

好的,这花了我几天的时间来解决,但这些是我为让它运行而做的事情。还有很多事情要做。

先决条件:

  • 在您的域中某处运行的 ADFS 2.0 服务。
  • 带有自签名证书或可在整个域中使用的证书的 IIS 7。
  • 安装了 Identity and Access(版本 1.0.2)扩展的 Visual Studio 2012。
  • 设置为在 IIS 上运行的 MVC4 Web 应用程序。
  • 确保将自签名证书添加到站点,以便您可以通过 https 访问它。
  • 您可能需要调整计算机的防火墙设置以允许访问 ADFS 2.0 服务。

  • 开发工作站上

    在您的 MVC4 项目中
  • 通过右键单击您的 Web 项目打开身份和访问对话框。
  • 选择使用企业身份提供程序(例如 ADFS2)
  • 输入 STS 元数据文档的路径,例如https://{ADFS 服务器路径}/FederationMetadata/2007-06/FederationMetadata.xml
  • 输入您的应用程序的领域,例如https://{WEB 应用程序 URL}/
  • 尾部斜杠有所不同。
  • 通过接受这些更改退出对话框。

  • 将以下代码添加到您的项目中
    using System;
    using System.IdentityModel.Services;
    
    namespace NAMESPACE
    {
        public class FixedWsFederationAuthenticationModule : WSFederationAuthenticationModule
        {
            public override void RedirectToIdentityProvider(string uniqueId, string returnUrl, bool persist)
        {
            //This corrects WIF error ID3206 "A SignInResponse message may only redirect within the current web application:"
            //First Check if the request url doesn't end with a "/"
            if (!returnUrl.EndsWith("/"))
            {
                //Compare if Request Url +"/" is equal to the Realm, so only root access is corrected
                //https://localhost/AppName plus "/" is equal to https://localhost/AppName/
                //This is to avoid MVC urls
                if (String.Compare(System.Web.HttpContext.Current.Request.Url.AbsoluteUri + "/", base.Realm, StringComparison.InvariantCultureIgnoreCase) == 0)
                {
                    //Add the trailing slash
                    returnUrl += "/";
                }
            }
            base.RedirectToIdentityProvider(uniqueId, returnUrl, persist);
        }
    }
    }
    

    在 ADFS 2.0 服务器上
  • 如果您使用自签名证书,请导航到您的 Web 应用程序 https://{WEB APPLICATION URL}/并将区域更改为受信任站点。
  • 在浏览器的地址栏中,您应该可以右键单击证书并进行安装(您只能安装来自受信任站点的证书) 证书需要安装在受信任的根权限-> 注册表下,
  • 打开 ADFS 控制台,添加 Relying Party Trusts,使用 Federated Metadata 地址 https://{WEB APPLICATION URL}/FederationMetadata/2007-06/FederationMetadata.xml

  • 添加一些自定义规则

    MVC4 需要这些规则来创建一个可用的 ClaimsPrincipal

    在 Name 属性上添加传递规则。

    而这 2 个自定义规则
    => issue(Type = "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier", Value = "true");
    
    => issue(Type = "http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider", Value = "true");
    

    关于asp.net-mvc-4 - 如何使用商业身份提供者(例如 ADFS2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13410658/

    相关文章:

    asp.net - 如何在 SessionSecurityTokenCreated 时有条件地设置 FedAuth cookie SessionToken.IsPersistent

    c# - 联合身份 token 复制字符以关闭标签(cookie)(使用 Thinktecture Identity Server)

    asp.net-mvc-4 - ClaimsAuthorizationManager.CheckAccess : Handling false without going to login page

    java - PUT 操作的 Android JSON api 问题

    jquery - 在 ASP.NET MVC 4 中使用 jQuery Mobile 1.3 面板时的双滚动条

    apache - apache Shiro 可以用来构建身份提供者吗?

    WIF- ID1014 : The signature is not valid. 数据可能被篡改

    c# - 跨请求保留新声明

    asp.net-mvc - 在 MVC Controller 操作中获取 POST 请求的正文

    asp.net-mvc - 路由 MVC 4 应用程序 url - css 和 js 不起作用