I implemented sitecore website registration and login functionality.I am going share same thing so that you can get idea to implement external logn in any sitecore websites.
I implemented code on registration and login pop window on every pages of website,when user is valid and logged then pop will disappear .Again if login user session in killed, then
same registration&login pop will appear.
I am not going to share whole code, but some codes those explain some complex login for it so you will get good idea about it.
When we want to store new user detail from external login form as sitecore user profile in core DB, then extranal login page comes into picture.
// Register and create sitecore user profile in core Db.
// maintain a session if user is registered and give timeout 2 minutes ( you can increase as per need)
Sitecore.Diagnostics.Log.Info( " SignUpFormSumbit - error: " + Sitecore.Context.Site.Name + ex.Message.ToString(), this );
string userName = authUtils. GetUserNameForSitePerConfiguration (Email);
private bool ProcessRegistrationUser(registerUser signUpData)
{
bool result = false;
var userName = "";
var roleName = "";
var authUtils = new AuthenticationUtils();
//Check if the site is configured to use default password or not.
//UseDefaultPassword == true means user is registered without a password on the form, default apssword in the config setting will be used
string passwordToRegister = "";
if (authUtils.IsSiteSharedLogin())
{
//getting default password from a common setting config file
passwordToRegister = Common.GetSiteSettingValue(" CommonSetting.Web.DefaultPassword" );
}
else
{
passwordToRegister = signUpData.Password;
}
if (!string.IsNullOrWhiteSpace(signUpData.Email))
{
roleName = authUtils.GetRoleName();
if (!Sitecore.Security.Accounts.Role.Exists(roleName))
{
System.Web.Security.Roles.CreateRole(roleName);
Sitecore.Diagnostics.Log.Info("Created Role:" + roleName, this);
}
var role = Sitecore.Security.Accounts.Role.FromName(roleName);
Sitecore.Security.Accounts.User user = null;
userName = authUtils. GetUserNameForSitePerConfiguration( signUpData.Email );
if (!String.IsNullOrEmpty(userName))
{
var isAuthenticated = System.Web.Security.Membership.ValidateUser(userName, passwordToRegister);
if (isAuthenticated)
{
var existingUser = Sitecore.Security.Accounts.User.FromName( userName, true);
if (!existingUser.IsInRole(roleName))
{
existingUser.Roles.Add(role);
}
result = true;
Sitecore.Diagnostics.Log.Info(" Web Registration FormSubmit - User-Exists. SiteName:" + Sitecore.Context.Site.Name + "| UserName:" + userName, this);
}
}
else
{
//Create the user on primary domain
userName = authUtils.GetUserName( signUpData.Email, Sitecore.Context.Site.Domain.AccountPrefix );
//Create User
user = Sitecore.Security.Accounts.User.Create( userName, passwordToRegister);
Sitecore.Diagnostics.Log.Info(" Web Registration FormSubmit - SiteName:" + Sitecore.Context.Site.Name + "|UserName:" + userName + " Created for site.", this);
//Add User to the role
user.Roles.Add(role);
//Update profile of the user.
bool contactResult = UpdateProfile(signUpData, user, role);
if (contactResult)
{
result = true;
}
else
{
Sitecore.Diagnostics.Log.Error(" Web Registration FormSubmit - UnabletoUpdateContact. Site Name:" + Sitecore.Context.Site.Name + "|Username:" + userName, this);
authUtils.DeleteUser(userName, roleName);
result = false;
}
}
}
return result;
}
// Update sitecore user profiles method
private bool UpdateProfile(RegisterUser registerData, Sitecore.Security.Accounts.User sitecoreUser, Sitecore.Security.Accounts.Role sitecoreRole)
{
bool retVal = false;
try
{
var authUtils = new AuthenticationUtils();
string profileId = Sitecore.Context.Site.Properties.Get(authUtils.Profile_Id);
if (string.IsNullOrWhiteSpace(profileId))
{
profileId = Constants.UserLoginProfileID;
}
if (string.IsNullOrWhiteSpace(profileId))
{
Sitecore.Diagnostics.Log.Fatal("Profile Id missing in Constant File", this);
retVal = false;
}
sitecoreUser.Profile.ProfileItemId = profileId;
sitecoreUser.Profile.Email = registerData.Email;
sitecoreUser.Profile.SetCustomProperty("FirstName", registerData.Name);
sitecoreUser.Profile.SetCustomProperty("LastName", registerData.Surname);
sitecoreUser.Profile.SetCustomProperty("Profession", registerData.Profession);
sitecoreUser.Profile.Name = string.Format("{0} {1}", registerData.Name, registerData.Surname);
sitecoreUser.Profile.FullName = string.Format("{0} {1}", registerData.Name, registerData.Surname);
sitecoreUser.Profile.Save();
sitecoreUser.Profile.Reload();
retVal = true;
}
catch (Exception)
{
retVal = false;
}
return retVal;
// Some utility class helping us creating and updating user profiles in sitecore
public class AuthenticationUtils
{
public string Profile_Id = "profileId";
public string Use_Default_Password = "useDefaultPassword";
public string SharedLogin = "sharedLogin";
public string SharedDomain = "sharedDomain";
public string Extranet_Account_Prefix = "extranet\\";
public string Role_Name = @"{0}\User";
public string GetRoleName()
{
return string.Format(Role_Name, Sitecore.Context.Site.Name);
}
public string GetUserName(string emailAddress, string domain)
{
//make sure there is a slash at the very end all the time
if (!domain.EndsWith("\\"))
{
domain += "\\";
}
return string.Format("{0}{1}", domain, emailAddress);
}
/// <summary>
/// This method also verifies if the User Exists or not
/// </summary>
/// <param name="emailAddress"></param>
/// <returns></returns>
public string GetUserNameForSitePerConfiguration(string emailAddress)
{
var retUserName = "";
var siteDomain = Sitecore.Context.Site.Domain.AccountPrefix;
if (!string.IsNullOrEmpty(emailAddress) && !string.IsNullOrEmpty(siteDomain))
{
var _userName = GetUserName(emailAddress, siteDomain);
//If the domain name exists as per domain attribute in SiteDefinition us it
if (Sitecore.Security.Accounts.User.Exists(_userName))
{
retUserName = _userName;
}
else
{
//Else if the site is configured to have Shared Login, then check if user exists in all configured domains
var sharedLoginBool = IsSiteSharedLogin();
if (sharedLoginBool)
{
var sharedDomain = Sitecore.Context.Site.Properties.Get( SharedDomain);
if (!string.IsNullOrEmpty(sharedDomain))
{
_userName = GetUserName(emailAddress, sharedDomain);//string.Format("{0}{1}", GetAccountPrefix(), emailAddress);
if (Sitecore.Security.Accounts.User.Exists(_userName))
{
retUserName = _userName;
}
}
}
}
}
return retUserName;
}
public bool IsSiteSharedLogin()
{
var sharedLoginString = Sitecore.Context.Site.Properties.Get(SharedLogin);
var sharedLoginBool = (!String.IsNullOrEmpty(sharedLoginString) && sharedLoginString.ToLower() == "true") ? true : false;
return sharedLoginBool;
}
public bool IsDefaultPassword()
{
//Check if the site is configured to use default password or not for registration/login.
//UseDefaultPassword == true means user is registered without a password on the form, default apssword in the config setting will be used
var isDefaultPassword = false;
var passwordSetting = Sitecore.Context.Site.Properties.Get(Use_Default_Password);
if (!string.IsNullOrWhiteSpace(passwordSetting))
{
isDefaultPassword = System.Convert.ToBoolean(passwordSetting);
}
return isDefaultPassword;
}
/// <summary>
/// This method is used to revert user registration process in case of any errors
/// </summary>
/// <param name="userName"></param>
/// <param name="userRoleName"></param>
public void DeleteUser(string userName, string userRoleName)
{
try
{
if (!String.IsNullOrEmpty(userName) && Sitecore.Security.Accounts.User.Exists(userName))
{
var user = Sitecore.Security.Accounts.User.FromName(userName, true);
//delete created User
user.Delete();
//Log this into Sitecore
Sitecore.Diagnostics.Log.Info("Utilities.AuthenticationUtils DeleteUser - SiteName:" + Sitecore.Context.Site.Name + "|UserName:" + userName + "|Role:" + userRoleName, this);
}
}
catch (Exception ex)
{
//Log ex
Sitecore.Diagnostics.Log.Error("Utilities.AuthenticationUtils DeleteUser - SiteName:" + Sitecore.Context.Site.Name + "|UserName:" + userName + "|Role:" + userRoleName + "|Exception:" + ex.Message, this);
}
}
public bool ShouldIdentifyContact()
{
var shouldIdentifyContact = Sitecore.Configuration.Settings.GetSetting( " CommonSetting.Web.ShouldIdentifyContact ", " false" );
//SHouldIdentify COntact should be tru
return (!String.IsNullOrEmpty(shouldIdentifyContact) && shouldIdentifyContact.ToLower().CompareTo("true") == 0);
}
}
// Constant Class
public class constant {
public const string UserDefaultPassword = "b";
public const string UserLoginProfileID= " {89F6E8A8-21FB-4020-A936- E01661D0E7F3} "; // User Profile item in Core DB
public const string True = "true";
public const string False = "false";
public const string AlreadyRegister = "AlreadyRegister";
public const string websiteDomain = "demoWeb";
}
I will explain rest registration form functionality in next page.Click here ==>