扩展系统登录功能

时间:2012-01-07 03:40:52

标签: c1-cms

扩展C1登录功能的最佳做法是什么。希望通过使用多因素身份验证替换密码身份验证来将功能扩展到当前登录过程。我有兴趣了解关键概念并挂钩到C1,以便在完成多因素身份验证后允许用户身份验证。

1 个答案:

答案 0 :(得分:0)

你看过这个讨论http://compositec1.codeplex.com/discussions/268428了吗?这里有实现ILoginProvider接口的类的示例代码,在验证用户名/密码组合时会被调用。

public class LDAPLoginProvider : IFormLoginProvider
{
    public bool CanSetUserPassword
    {
            get { return false; }
    }

    public bool CanAddNewUser
    {
            get { return false; }


    public bool UsersExists
    {
            get { return DataFacade.GetData<IUser>().Any(); }
    }

    public IEnumerable<string> AllUsernames
    {
        get { return (from u in DataFacade.GetData<IUser>() select u.Username).ToList(); }
    }

    void SetUserPassword(string username, string password) 
    {
        throw new NotImplementedException();
    }

    void AddNewUser(string userName, string password, string group) 
    {
        throw new NotImplementedException(); 
    }

     LoginResult Validate(string username, string password)
     {
        var user =
            (from u in DataFacade.GetData<IUser>()
             where String.Compare(u.Username, username, StringComparison.InvariantCultureIgnoreCase) == 0
             select u).FirstOrDefault();

        if (user == null)
        {
            return LoginResult.UserDoesNotExist;
        }

        bool loginIsValid = false;

        try
        {
            var entry = new DirectoryEntry("LDAP://domain.com", username, password);
            object nativeObject = entry.NativeObject;
            loginIsValid = true;
        }
        catch (DirectoryServicesCOMException ex)
        {
            //not authenticated; reason why is in ex
        }
        catch (Exception ex)
        {
            //not authenticated due to some other exception
        }

        return loginIsValid ? LoginResult.Success : LoginResult.IncorrectPassword;
    }         
}

此处仍然需要在C1中创建用户,但是密码是针对外部源(此处为LDAP服务器)进行验证的。