API
Version 7.x > API > Allow Email and Username Authentication View modes: 
User avatar
Member
Member
gary-allixo - 3/12/2013 5:41:59 PM
   
Allow Email and Username Authentication
I've attempted to create a custom security handler per the kentico documentation.

I can see my method getting hit and step through the code but it doesn't seem to authenticate the user if an email is entered.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CMS.SettingsProvider;
using CMS.Security;
using CMS.SiteProvider;
using CMS.MembershipProvider;
using CMS.GlobalHelper;
using System.Web.Security;
using CMS.CMSHelper;
using CMS.WebAnalytics;

[CustomSecurityEvents]
public partial class CMSModuleLoader
{
private class CustomSecurityEventsAttribute : CMSLoaderAttribute
{
public override void Init()
{
SecurityEvents.Authenticate.Execute += new EventHandler<AuthenticationEventArgs>(Authenticate_Execute);
}

void Authenticate_Execute(object sender, AuthenticationEventArgs e)
{
if (e.User != null)
return;
else
{
var membershipProvider = new CMSMembershipProvider();
if (ValidationHelper.IsEmail(e.UserName))
{
var username = membershipProvider.GetUserNameByEmail(e.UserName);
if (e.Password == membershipProvider.GetPassword(username, null))
{
e.User = UserInfoProvider.GetUserInfo(username);
}
}
}
}
}
}


User avatar
Member
Member
gary-allixo - 3/18/2013 7:04:47 PM
   
RE:Allow Email and Username Authentication
Here is a Gist detailing how to allow the username or the email of the user for the login.

https://gist.github.com/redanthrax/5046de65a4218e1e5d4a


//CustomMembershipProvider.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CMS.CMSHelper;
using CMS.GlobalHelper;

public class CustomMembershipProvider : CMS.MembershipProvider.CMSMembershipProvider
{
public override bool ValidateUser(string username, string password)
{
if (ValidationHelper.IsEmail(username))
{
var customUsername = base.GetUserNameByEmail(username);
if (!string.IsNullOrEmpty(customUsername))
username = customUsername;
}


return base.ValidateUser(username, password);
}
}


//CustomUserInfoProvider.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CMS.SiteProvider;
using CMS.GlobalHelper;
using CMS.MembershipProvider;

public class CustomUserInfoProvider : UserInfoProvider
{
protected override UserInfo GetUserInfoInternal(string userName)
{
if (ValidationHelper.IsEmail(userName))
{
var membershipProvider = new CMSMembershipProvider();
var customUserName = membershipProvider.GetUserNameByEmail(userName);
if (!string.IsNullOrEmpty(customUserName))
userName = customUserName;
}

return base.GetUserInfoInternal(userName);
}
}

//CMSAppBase.cs
/// <summary>
/// Application start event handler.
/// </summary>
public static void CMSApplicationStart()
{
#if DEBUG
// Set debug mode
SystemHelper.IsWebProjectDebug = true;
#endif

UserInfoProvider.ProviderObject = new CustomUserInfoProvider();
}

//Web.config
<!--<add name="CMSProvider" type="CMS.MembershipProvider.CMSMembershipProvider" connectionStringName="CMSConnectionString" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="true" passwordFormat="Hashed"/>-->
<add name="CMSProvider" type="CustomMembershipProvider" connectionStringName="CMSConnectionString" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="true" passwordFormat="Hashed"/>


Definitely giving credit to:
Juraj Ondrus
Customer Care Manager
Kentico Support