Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· OpenAIAi2 WildChat-4.8M (AI2)

asp.net 集成WsFederation示例

  1. User

    asp.net 集成WsFederation示例

  2. Assistant

    下面是一个ASP.NET集成WsFederation的示例代码:

    1. 首先,在Web.config文件中配置WsFederation认证相关的设置:
    <system.identityModel>
      <identityConfiguration>
        <audienceUris>
          <add value="https://localhost:44300/" />
        </audienceUris>
        <issuerNameRegistry type="System.IdentityModel.Tokens.ConfigurationBasedIssuerNameRegistry">
          <trustedIssuers>
            <add thumbprint="YourThumbprint" name="YourIssuerName" />
          </trustedIssuers>
        </issuerNameRegistry>
        <securityTokenHandlers>
          <add type="System.IdentityModel.Tokens.Saml2SecurityTokenHandler, System.IdentityModel.Tokens.Saml2SecurityTokenHandler" />
          <add type="System.IdentityModel.Tokens.SamlSecurityTokenHandler, System.IdentityModel.Tokens.SamlSecurityTokenHandler" />
        </securityTokenHandlers>
      </identityConfiguration>
    </system.identityModel>
    
    <system.identityModel.services>
      <federationConfiguration>
        <cookieHandler requireSsl="false" />
        <wsFederation passiveRedirectEnabled="true" issuer="https://your.sts/issue" realm="https://localhost:44300/" reply="https://localhost:44300/" requireHttps="false" />
      </federationConfiguration>
    </system.identityModel.services>
    
    <system.web>
      <authentication mode="None" />
      <authorization>
        <deny users="?" />
      </authorization>
    </system.web>
    

    在以上配置中,需要根据具体情况修改issuerNameRegistry中的配置和wsFederation中的issuer、realm和reply的值。

    1. 在Global.asax文件中的Application_Start方法中注册相关的中间件:
    protected void Application_Start()
    {
        // 省略其他代码...
        AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.NameIdentifier;
        FederatedAuthentication.FederationConfigurationCreated += FederatedAuthentication_FederationConfigurationCreated;
    }
    
    private void FederatedAuthentication_FederationConfigurationCreated(object sender, FederationConfigurationCreatedEventArgs e)
    {
        e.FederationConfiguration.IdentityConfiguration.AudienceRestriction.AllowedAudienceUris.Add(new Uri("https://localhost:44300/"));
    }
    
    1. 在登录页面的代码中添加登录按钮并处理回调:
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.IsAuthenticated)
        {
            Response.Redirect("~/");
        }
    }
    
    protected void LoginButton_Click(object sender, EventArgs e)
    {
        var signInRequestMessage = new SignInRequestMessage(new Uri("https://your.sts/issue"), "https://localhost:44300/");
        signInRequestMessage.SetParameter("wtrealm", "https://localhost:44300/");
        Response.Redirect(signInRequestMessage.WriteQueryString());
    }
    
    1. 在回调页面的代码中处理登录成功后的逻辑:
    protected void Page_Load(object sender, EventArgs e)
    {
        var signInResponseMessage = (SignInResponseMessage)WSFederationMessage.CreateFromUri(Request.Url);
        var identity = FederatedAuthentication.FederationConfiguration.IdentityConfiguration.SecurityTokenHandlers.ReadToken(new XmlTextReader(new StringReader(signInResponseMessage.Result)));
    
        // 在这里进行登录成功后的逻辑处理
        var claimsIdentity = identity as ClaimsIdentity;
        if (claimsIdentity != null)
        {
            var nameIdentifierClaim = claimsIdentity.FindFirst(ClaimTypes.NameIdentifier);
            var nameClaim = claimsIdentity.FindFirst(ClaimTypes.Name);
            if (nameIdentifierClaim != null && nameClaim != null)
            {
                FormsAuthentication.SetAuthCookie(nameIdentifierClaim.Value, false);
                Response.Redirect("~/");
            }
        }
    }
    

    以上代码中的"https://your.sts/issue"是STS的地址,需要根据实际情况进行修改。

    这个示例演示了如何在ASP.NET中集成WsFederation认证,并在登录成功后获取用户的相关信息。

  3. User

    wsFederation reply回来还是没有认证成功

  4. Assistant

    如果在集成WsFederation时,回调后仍然没有认证成功,可能有以下一些常见原因:

    1. 检查STS的配置:确认STS的配置中的Realm和Reply URL是否正确配置为当前应用程序的URL。如果不正确,STS将无法正确回调到应用程序。

    2. 检查SSL设置:如果在回调URL中使用了SSL(https),请确保在Web.config中将requireHttps设置为true。

    3. 检查受信任的颁发者:在Web.config的issuerNameRegistry部分中,确保正确配置了信任的颁发者的thumbprint和name。这些值应与STS的配置匹配,以确保系统信任来自颁发者的令牌。

    4. 检查应用程序的配置:确认应用程序的Web.config文件中的wsFederation配置是否正确。特别是,在issuer、realm和reply URL中使用正确的值。

    5. 检查身份验证配置:请确保在Web.config文件中将<system.web>中的<authentication mode="None" />配置为None。

    6. 检查登录逻辑:在登录按钮的事件处理程序中,确保使用正确的STS URL创建SignInRequestMessage。确保设置正确的wtrealm参数,并使用Response.Redirect将用户重定向到STS进行身份验证。

    如果仍然无法解决问题,请尝试使用Fiddler或浏览器开发者工具检查发生的网络请求和响应,以确定是否收到了正确的身份验证令牌,并排除其他可能的问题。

  5. User

    azure application gateway使用WsFederation,认证成功还是回到webapp的域名,不是绑定application gateway的域名

  6. Assistant

    在使用Azure Application Gateway和WsFederation集成时,如果认证成功后回到了Web App的域名,而不是绑定到Application Gateway的域名,可能是由于以下原因导致:

    1. 检查Azure Application Gateway的HTTP设置:确保已正确配置Application Gateway的HTTP设置,将后端地址池指向正确的Web App,并将后端HTTP设置中的主机名称设置为Web App的域名。这样,认证成功后,Application Gateway会将请求转发到正确的Web App。

    2. 检查Web App的回调URL:确保Web App的回调URL设置为Application Gateway的域名,而不是Web App的域名。您可以通过在Web App的身份验证设置中配置回调URL来实现这一点。

    3. 检查STS的回调URL配置:在STS的配置中,确保将回调URL设置为Application Gateway的域名。这样,认证成功后,STS将返回令牌到Application Gateway的域名,并由Application Gateway转发给Web App。

    4. 检查DNS解析:确保正确配置了域名解析,将Application Gateway的域名解析到Application Gateway的公共IP地址。

    如果您已经确认上述设置均正确配置,但仍然无法解决问题,请检查Application Gateway的后端HTTP设置中的主机名称,确保域名与Web App的域名完全一致。如果问题仍然存在,您可以尝试更新Application Gateway的规则和HTTP设置,或联系Microsoft Azure支持团队获取更进一步的帮助。