asp.net-mvc - MVC 5,WIF(System.IdentityModel)和ADFS实际未进行身份验证,联合注销时出错

标签 asp.net-mvc visual-studio-2013 .net-4.5 wif adfs

我正在努力在Visual Studio 2013中创建基于声明的MVC网站。
到目前为止,我学到了一些东西:

System.Identity在,Microsoft.Identity在
许多教程(包括Microsoft的4.5指南)已过时。例如,我不认为对项目模板生成的.config文件进行任何更改对于添加模块/处理程序或其他任何操作都是不必要的。
与2012年一样,Visual Studio 2013中没有Microsoft内置/内置STS
Thinktecture的EmbeddedSTS插件经常被推荐并且听起来很酷,但是*:// EmbeddedSTS /不能解析(??我不明白)。另外,指向其IdentityServer v2-的二进制链接当前已断开(?)
ADFS功能需要Windows Server 2012,域和自签名证书-如果您以前曾经做过,那么就不会太难,但是如果没有,则学习曲线会很艰难。
ADFS需要SSL-Visual Studio 2013 / IIS Express 8轻松支持SSL站点,只需确保端口号在以下范围内:44300-44398
ADFS经理依赖方界面建议了一些示例,它们涉及“ sts”和“ adfs / ls”以及我认为具有误导性的内容。确实,它们应该只指向您的应用程序(例如https://localhost:44300)。尽管我的还不能正常工作,所以这可能与我的错误有关。
创建新的Visual Studio Web应用程序项目后,将无法使用任何工具来更改身份验证机制。只需从一个新项目开始,然后将身份验证更改为组织帐户(就我而言,对于内部部署)。为了完成此向导,必须安装STS(例如ADFS安装)并可以访问它。
如果您因为没有权限将计算机加入域中的权限而与VM中的测试ADFS DC一起被黑客入侵,请使用hosts文件将VM的IP的DNS覆盖为期望的域名。
首先解决登录问题时,“每次登录都需要用户提供凭据”将很有帮助。
如果您没有任何“索赔规则”,我认为不会通过任何索赔甚至身份。

1)出什么问题了,我的应用仍然认为用户未通过身份验证?
如果用户通过身份验证,那么我的https://localhost:44300/Default/Index/操作应该显示User.Identity的详细信息(我也尝试过Thread.CurrentPrincipal.Identity)。我有一个登录操作链接,生成为:

var signIn = new SignInRequestMessage(new Uri("https://dc.ad.dev.local/adfs/ls/"), "https://localhost:44300");

return new RedirectResult(signIn.WriteQueryString());

单击此链接确实使我进入了ADFS登录页面。登录使我回到我的应用程序。观察Chrome Bug中保留的网络活动,可以看到我有一条RequestSecurityTokenResponse消息被发回到应用程序中,但是该应用程序的User.Identity仍未通过身份验证。
我配置了一个“索赔规则”:将“将收到的索赔”从“ Windows帐户名称”转换为“名称ID”作为“临时标识符”。我看到嗅到的<saml:NameIdentifier Format="urn:oasis:names:tc:SAML:2.0:nameid-format:transient">DevAD\jdoe</saml:NameIdentifier>中表示的POST。我尝试了许多其他的“索赔规则”,但仍然没有通过身份验证。
我没有任何用于吸收索赔的自定义代码。我希望框架将它与任何应用程序URL的令牌截获的POST一起自动转换为User.Identity,也许是由Startup.Auth.cs中此向导生成的代码启动的:
app.UseActiveDirectoryFederationServicesBearerAuthentication(
    new ActiveDirectoryFederationServicesBearerAuthenticationOptions
    {
        Audience = ConfigurationManager.AppSettings["ida:Audience"],
        MetadataEndpoint = ConfigurationManager.AppSettings["ida:AdfsMetadataEndpoint"]
    });

但我的一部分对此表示怀疑。这是正确的吗? MVC WIF除了默认路由URL之外,是否还存在一条我应该使用的特殊已知路由,以接受此类登录帖子?
2)如何成功注销?
我还有一个注销操作:
WSFederationAuthenticationModule.FederatedSignOut(new Uri("https://dc.ad.dev.local/adfs/ls/"), new Uri(Url.Action("Index", null, null, Request.Url.Scheme)));

但是在此https://dc.ad.dev.local/adfs/ls?wa=wsignout1.0&wreply=https%3a%2f%2flocalhost%3a44300%2f页面上,“发生错误”。事件查看器显示#364:“联合身份验证被动请求期间遇到错误”。
Protocol Name: 
wsfed 

Relying Party: 

Exception details: 
System.ArgumentException: An item with the same key has already been added.
   at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   at Microsoft.IdentityServer.Web.Protocols.WSFederation.WSFederationProtocolHandler.AddSignoutSessionInformation(WSFederationSignOutContextBase context)
   at Microsoft.IdentityServer.Web.Protocols.WSFederation.WSFederationProtocolHandler.ProcessSignOut(WSFederationSignOutContext context)
   at Microsoft.IdentityServer.Web.PassiveProtocolListener.ProcessProtocolSignoutRequest(ProtocolContext protocolContext, PassiveProtocolHandler protocolHandler)
   at Microsoft.IdentityServer.Web.PassiveProtocolListener.ProcessProtocolRequest(ProtocolContext protocolContext, PassiveProtocolHandler protocolHandler)
   at Microsoft.IdentityServer.Web.PassiveProtocolListener.OnGetContext(WrappedHttpListenerContext context)

我的ADFS服务>证书均设置为同一证书,我认为是正确的。
================
顺便说一下,以下是应该被动发布到应用程序的内容,对吗?而且,它会自动吸收吗?
<t:RequestSecurityTokenResponse xmlns:t="http://schemas.xmlsoap.org/ws/2005/02/trust">
    <t:Lifetime>
        <wsu:Created xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">2014-07-28T14:29:47.167Z</wsu:Created>
        <wsu:Expires xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">2014-07-28T15:29:47.167Z</wsu:Expires>
    </t:Lifetime>
    <wsp:AppliesTo xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy">
        <wsa:EndpointReference xmlns:wsa="http://www.w3.org/2005/08/addressing">
            <wsa:Address>https://localhost:44300/</wsa:Address>
        </wsa:EndpointReference>
    </wsp:AppliesTo>
    <t:RequestedSecurityToken>
        <saml:Assertion MajorVersion="1" MinorVersion="1" AssertionID="_e2399a27-acac-4390-aa8a-556f41fec2f2" Issuer="http://dc.ad.dev.local/adfs/services/trust" IssueInstant="2014-07-28T14:29:47.167Z" xmlns:saml="urn:oasis:names:tc:SAML:1.0:assertion">
            <saml:Conditions NotBefore="2014-07-28T14:29:47.167Z" NotOnOrAfter="2014-07-28T15:29:47.167Z">
                <saml:AudienceRestrictionCondition>
                    <saml:Audience>https://localhost:44300/</saml:Audience>
                </saml:AudienceRestrictionCondition>
            </saml:Conditions>
            <saml:AttributeStatement>
                <saml:Subject>
                    <saml:NameIdentifier Format="urn:oasis:names:tc:SAML:2.0:nameid-format:transient">DevAD\jdoe</saml:NameIdentifier>
                    <saml:SubjectConfirmation>
                        <saml:ConfirmationMethod>urn:oasis:names:tc:SAML:1.0:cm:bearer</saml:ConfirmationMethod>
                    </saml:SubjectConfirmation>
                </saml:Subject>
                <saml:Attribute AttributeName="name" AttributeNamespace="http://schemas.xmlsoap.org/ws/2005/05/identity/claims">
                    <saml:AttributeValue>jdoe</saml:AttributeValue>
                </saml:Attribute>
                <saml:Attribute AttributeName="givenname" AttributeNamespace="http://schemas.xmlsoap.org/ws/2005/05/identity/claims">
                    <saml:AttributeValue>John Doe</saml:AttributeValue>
                </saml:Attribute>
                <saml:Attribute AttributeName="upn" AttributeNamespace="http://schemas.xmlsoap.org/ws/2005/05/identity/claims">
                    <saml:AttributeValue>jdoe@ad.dev.local</saml:AttributeValue>
                </saml:Attribute>
            </saml:AttributeStatement>
            <saml:AuthenticationStatement AuthenticationMethod="urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport" AuthenticationInstant="2014-07-28T14:29:47.152Z">
                <saml:Subject>
                    <saml:NameIdentifier Format="urn:oasis:names:tc:SAML:2.0:nameid-format:transient">DevAD\jdoe</saml:NameIdentifier>
                    <saml:SubjectConfirmation>
                        <saml:ConfirmationMethod>urn:oasis:names:tc:SAML:1.0:cm:bearer</saml:ConfirmationMethod>
                    </saml:SubjectConfirmation>
                </saml:Subject>
            </saml:AuthenticationStatement>
            <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
                <ds:SignedInfo>
                    <ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
                    <ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" />
                    <ds:Reference URI="#_e2399a27-acac-4390-aa8a-556f41fec2f2">
                        <ds:Transforms>
                            <ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
                            <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
                        </ds:Transforms>
                        <ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256" />
                        <ds:DigestValue>+ZDduF0CKxXq7P+diyAXN51mo549pvwo3BNCekWSEpk=</ds:DigestValue>
                    </ds:Reference>
                </ds:SignedInfo>
                <ds:SignatureValue>VMjCbSZXw3YROHYQ1eCYH5D9UQl1tzqZ9Nw99FUK78A8TSLs1ns3G8PE1d1Z1db2KKpbnzExXSXG2elP3Z69OejSWjsywIFTPeGcbGk4BvrV4ZcHGCbYKN0Wg5pySMEqm4LV1E5k+32kuALveLi5fkQROyXudquvVRgYrgu7XBsfr96Uvqo1yWmAzhhpEorfe4Z0p4RurKRpS7IsrI9SkssGOdQV/89NQelIZSZzOEMfay/AxewBbQ8C46g/4NgygaaPsG8X52EFVftzFY0BM8k+aMMUiKrJ0Xo7tJCMxJLcQ3aJdLBRNybHaklFgtln0ZCSlYylglUjUZ5d66jGcg==</ds:SignatureValue>
                <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
                    <X509Data>
                        <X509Certificate>MIIC7jCCAdagAwIBAgIQLB+dBr0GI75OvLElC1HZHTANBgkqhkiG9w0BAQsFADAzMTEwLwYDVQQDEyhBREZTIFNpZ25pbmcgLSBkYy5hZC5lbnRlcnByaXNlZGV2LmxvY2FsMB4XDTE0MDcyNDIxMTMxM1oXDTE1MDcyNDIxMTMxM1owMzExMC8GA1UEAxMoQURGUyBTaWduaW5nIC0gZGMuYWQuZW50ZXJwcmlzZWRldi5sb2NhbDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALvkkbfrr5YZWNkfv7LFQlVj3qTcfduRltKfAGiK/NOXNp498uMY+zhUBtiPU1woJhsoqfahgQpy3YJhIPsxbVGSXjAGcFVcUq03V2xVJB6+tW1Ny+/lqiXrdvYAHcZvqpeG/tnh5/hGi/mapd2oMxWIWkyRdztJrN+BCzUi4mm80bYrYX8liuDAcJEk5EYh73gaOwzIyUAZvOXwX1UWh9FA8j2mIMyv3b4SgjFQMPT+Fyw4L1cX+3u/PhGlVRSUEAu+igfMxM1JTco+3HMfQuBZLGd8YXhig+6WbIGlcGUhHEdNSr9ymljJBaps1JfGZk7Rj/7hYeHCXbl3mKK7yFUCAwEAATANBgkqhkiG9w0BAQsFAAOCAQEAU5gYs7BZZBrrm+eDZM5pTaQfnwyeHYWHe9D2UDweWTNjj9KVC2sucUI2K9MXzA3kZOP7UIvnLmHvxG7cnPen095NBIyYYDjzvlImGwq297m6cz0m2ZxkBGMKq9bVSPoVOgDrX0c+e2wFjRgVojd1bWm9fbMvIUWn8KyMQMquXmyJxX5sPxpMfm95yheyh6q67VzgWF9TcUp6jNdmMcRowHWnQ9UVYf1UEMcPUlaljARVQYNQjyHjrcFaRkxF57EkjO3e263KIe0knVNoz8W57prXJLOHOqSe2f4kSKUdU+Xt8XQbJ77xHPdSOoW8NwKZVL7/9TrfVJ6pi1Ob/+LrAA==</X509Certificate>
                    </X509Data>
                </KeyInfo>
            </ds:Signature>
        </saml:Assertion>
    </t:RequestedSecurityToken>
    <t:TokenType>urn:oasis:names:tc:SAML:1.0:assertion</t:TokenType>
    <t:RequestType>http://schemas.xmlsoap.org/ws/2005/02/trust/Issue</t:RequestType>
    <t:KeyType>http://schemas.xmlsoap.org/ws/2005/05/identity/NoProofKey</t:KeyType>
</t:RequestSecurityTokenResponse>

===============
以下是定义的索赔。正如@nzpcmad所建议的那样,第二个请求现在从“ Active Directory”中“将LDAP属性作为声明发送”,以将“ SAM-Account-Name”作为“名称”发送,将“ Display Name”作为“给定名称”发送,并将“ User-委托人名称”为“ UPN”。尽管应用程序在被动帖子中收到声明,但User.Identity.IsAuthenticated仍然为false,其他User.Identity数据也为空白。

最佳答案

您的观察结果几乎正确。

请注意:

ADFS在Server 2008 R2、2012和2012 R2上运行。

看看Use the On-Premises Organizational Authentication Option (ADFS) With ASP.NET in Visual Studio 2013

它准确描述了您要执行的操作。

特别是请看权利要求。

您会看到它使用“发送LDAP属性”而不是您使用的“转换”。

关于asp.net-mvc - MVC 5,WIF(System.IdentityModel)和ADFS实际未进行身份验证,联合注销时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24959357/

相关文章:

javascript - MVC3 AJAX 将数据传递到 Controller 。已提交两次

asp.net-mvc - ASP.NET Core MVC 2.2 在模型中失去值(value)

c++ - 有没有办法在编译时加载 omp.h,因此机器永远不需要在运行时获取它?

visual-studio-2012 - Cmake Visual Studio 2013 错误

wpf - WPFtoolkit 与 .Net 4.5 兼容吗?

asp.net - Linq to Entities 通过位置映射表自定义排序

asp.net-mvc - Visual Studio 2015 CE 智能感知持续关闭

c# - Visual Studio 安装程序 UI 集成

C# .NET4.5.1 正则表达式奇怪?

c# - 为什么检测到正在更改的对象属性但未检测到刷新对象