IMG-LOGO

How to get user password in DNN?

andy - 18 Sep, 2013 6708 Views 0 Comment

If you are a DNN developer, you may wonder how you can retrieve a password from an existing DNN user account. Unfortunately in DNN site, you can not view the existing user password. The only way you can do is either change their existing password to the new one. To overcome it, We have include a pretty simple code snippet to retrieve the password by using DNN framework.

Firstly, make sure you import the following namespaces in your code

using DotNetNuke.Entities.Users;

We created 3 functions available for you to choose:

/** The first function is based on User ID, Note: the passwordAnswer can be left blank or empty string, if user does not specify any password question answer **/
private string GetPassword(int portalID, int userID, string passwordAnswer) {
    string password = "";
    UserInfo objUser = UserController.GetUserById(portalID, userID);
    if (objUser != null) {
        password = UserController.GetPassword(ref objUser, passwordAnswer);
    }
    return password;
}

/** The second function is based on Username, Note: the passwordAnswer can be left blank or empty string, if user does not specify any password question answer **/
private string GetPassword(int portalID, string username, string passwordAnswer) {
    string password = "";
    UserInfo objUser = UserController.GetUserByName(portalID, username);
    if (objUser != null) {
        password = UserController.GetPassword(ref objUser, passwordAnswer);
    }
    return password;
 }

/** The last function is based on current user login, Note: the passwordAnswer can be left blank or empty string, if user does not specify any password question answer **/
private string GetCurrentUserPassword() {
    string password = "";
    string passwordAnswer = "";
    UserInfo objUser = UserController.GetCurrentUserInfo();
    objUser = UserController.GetCurrentUserInfo();
    if (objUser != null) {
        password = UserController.GetPassword(ref objUser, passwordAnswer);
    }
    return password;
}

To use the function is pretty simple, see the following example:

string password1 = GetPassword(1, 1, "");
string password2 = GetPassword(1, "Username", "");
string password3 = GetCurrentUserPassword();

Comments

There are no comments available.

Write Comment
0 characters entered. Maximum characters allowed are 1000 characters.

Related Articles

How to change copyright text in DNN?

In this article, I am going to show you how to change the copyright text in DNN. Usually, a copyright text is located under the footer of the site template.