【问题标题】:ASP.NET web forms - how to combine WIF authentification with membership provider and role providerASP.NET Web 表单 - 如何将 WIFI 身份验证与成员资格提供者和角色提供者相结合
【发布时间】:2014-01-22 10:40:42
【问题描述】:

我在 .NET 4.5 的 ASP.NET Web 窗体中使用带有窗体身份验证的 Windows 身份基础 如何将 WIF 表单身份验证与我在 web.config 中定义的自定义成员资格提供程序和自定义角色提供程序相结合?

我想使用我的自定义会员提供程序从 SQL DB 加载其他用户信息,例如电子邮件、生日、头像 iamge。 我想使用我的自定义角色提供程序从 SQL DB 中为经过身份验证的用户获取所有角色。

我的认证方法Authenticate(userName, password)是从Login.aspx LoginButtonClick调用的:

    public static ClaimsPrincipal Authenticate(string userName, string password)
    {
        var principal = AuthenticateWindowsUser(userName, password);
        var inputIdentity = (WindowsIdentity)principal.Identity;

        var outputIdentity = new ClaimsIdentity(inputIdentity.AuthenticationType);
        outputIdentity.AddClaim(new Claim(ClaimTypes.Name, inputIdentity.Name));
        return new ClaimsPrincipal(outputIdentity);
    }

    private static WindowsPrincipal AuthenticateWindowsUser(string userName, string password)
    {
        try
        {
            SecurityToken securityToken = new UserNameSecurityToken(userName, password);
            var handlers = FederatedAuthentication.FederationConfiguration.IdentityConfiguration.SecurityTokenHandlers;

            //Uses default WindowsUserNameSecurityTokenHandler
            return new WindowsPrincipal((WindowsIdentity)handlers.ValidateToken(securityToken)[0]);
        }
        catch (SecurityTokenValidationException ex)
        {
            ShowException(ex);
        }
    }

【问题讨论】:

    标签: asp.net wif membership-provider roleprovider


    【解决方案1】:

    假设提供的代码对你有用,它应该是

    public static ClaimsPrincipal Authenticate(string userName, string password)
    {
        var principal = AuthenticateWindowsUser(userName, password);
        var inputIdentity = (WindowsIdentity)principal.Identity;
    
        var outputIdentity = new ClaimsIdentity(inputIdentity.AuthenticationType);
        outputIdentity.AddClaim(new Claim(ClaimTypes.Name, inputIdentity.Name));
    
        // other information from the membership provider
        var user = Membership.GetUser( userName ) );
        outputIdentity.AddClaim( new Claim( ClaimTypes.Email, user.Email ) );
        ...
    
        // roles from role provider
        foreach ( string role in Roles.GetRolesForUser( userName ) )
           outputIdentity.AddClaim( new Claim( ClaimTypes.Role, role ) );
    
        return new ClaimsPrincipal(outputIdentity);
    }
    

    【讨论】:

    • @paul:您为什么要尝试使用此代码通过您的 adfs 声明?似乎您尝试将错误问题的答案应用于您的场景。另外,您似乎需要创建自己的问题。
    • @Wiktor,抱歉,ADFS 和声明的新手..我使用上面的代码对我的用户进行身份验证,但也想检索作为声明传递的 LDAP 属性,作为 AD FS 服务器依赖部分上的设置相信。我在这里为此创建了一个新问题:stackoverflow.com/questions/29084449/… 我删除了我之前的评论,因为在这里发布它是不正确的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-11
    • 1970-01-01
    • 1970-01-01
    • 2013-05-16
    • 1970-01-01
    • 2011-07-25
    相关资源
    最近更新 更多