Writing a Simple Authentication Provider for DNN 4.3+
Fareed
posted about creating custom authentication module for DNN4. With 4.3; they have again changed the authentication implementation. For 4.3; you will be doing these steps instead
- Reference Dotnetnuke and Dotnetnuke.Provider.AspNetProvider assemblies
- Inherit your auth provider from DotNetNuke.Security.Membership.AspNetMembershipProvider
- Override DotNetNuke.Entities.Users.UserInfo UserLogin(int portalId, string username, string password, string verificationCode, ref DotNetNuke.Security.Membership.UserLoginStatus loginStatus)
- For successful login; set loginStatus = DotNetNuke.Security.Membership.UserLoginStatus.LOGIN_SUCCESS
- To return UserInfo; use the following
DotNetNuke.Security.Authentication.UserController uc = new DotNetNuke.Security.Authentication.UserController();
return uc.GetUser(username);
- To configure your provider you need to specify it in web.config' configuration/dotnetnuke/members/providers section
Here are some helper methods; that you might need
using DotNetNuke.Entities.Portals;
using DotNetNuke.Security.Roles;
using DotNetNuke.Entities.Users;
#region DNN helper methods
int getDNNPortalID()
{
PortalSettings portalSettings = PortalController.GetCurrentPortalSettings();
return portalSettings.PortalId;
}
bool checkUserInPortal(string userName, out int userID)
{
userID = -1;
bool dnnUserExists = false;
try
{
try
{
UserController uc = new UserController();
UserInfo ui = uc.GetUserByUsername(this.getDNNPortalID(), userName);
if (null != ui)
{
dnnUserExists = true;
userID = ui.UserID;
}
}
catch { }
}
catch { }
return dnnUserExists;
}
DotNetNuke.Security.Authentication.UserInfo createDNNUserInfo(
string userName, string password, string emailPostFix)
{
DotNetNuke.Security.Authentication.UserInfo ui =
new DotNetNuke.Security.Authentication.UserInfo();
ui.AffiliateID = -1;
ui.DistinguishedName = userName + emailPostFix;
ui.DisplayName = userName;
ui.GUID = Guid.NewGuid().ToString();
ui.FirstName = userName;
ui.FullName = userName;
ui.IsSuperUser = false;
ui.LastName = " ";// userName;
ui.Membership.Approved = true;
ui.Membership.Email = userName + emailPostFix;
ui.Membership.Password = password;
ui.Membership.Username = userName;
//ui.Name = userName; Readonly
ui.PortalID = this.getDNNPortalID();
ui.PrincipalName = userName + emailPostFix;
//ui.Profile.FullName = userName; Readonly
ui.Profile.FirstName = userName;
ui.Profile.LastName = " ";// userName;
ui.Username = userName;
return ui;
}
void giveDNNRoleToDNNUser(int userID, string userName, string roleName)
{
RoleController rc = new RoleController();
RoleInfo ri = rc.GetRoleByName(0, roleName);
try
{
rc.AddUserRole(
this.getDNNPortalID(),
userID, ri.RoleID,
DotNetNuke.Common.Utilities.Null.NullDate);
}
catch (Exception ex)
{
//log ex
}
}
bool createUserInDotNetNuke(string userName, string password, string[] roleNames, string emailPostFix)
{
DotNetNuke.Security.Authentication.UserInfo ui =
this.createDNNUserInfo(userName, password, emailPostFix);
DotNetNuke.Security.Authentication.UserController uc =
new DotNetNuke.Security.Authentication.UserController();
try
{
DotNetNuke.Security.Membership.UserCreateStatus s =
uc.AddDNNUser(ui);
if ( s.Equals(DotNetNuke.Security.Membership.UserCreateStatus.Success) )
{
DotNetNuke.Security.Membership.AspNetMembershipProvider ap =
new DotNetNuke.Security.Membership.AspNetMembershipProvider();
UserInfo ui2 = ap.GetUserByUserName(this.getDNNPortalID(), ui.Username, true);
int insertedUserID = ui2.UserID;
#region Giving roles to newly created user
if (null != roleNames)
{
foreach (string roleName in roleNames)
{
this.giveDNNRoleToDNNUser(insertedUserID, userName, roleName);
}
}
#endregion
return true;
}
}
catch (Exception ex)
{
//log ex
}
return false;
}
#endregion