首页 文章

ASP.NET Web表单 - 如何将WIF身份验证与成员资格提供程序和角色提供程序相结合

提问于
浏览
0

我在.NET Web中的ASP.NET Web窗体中使用窗口身份基础和表单身份验证如何将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);
        }
    }

1 回答

  • 2

    假设提供的代码适合您,它应该是

    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);
    }
    

相关问题