Maybe sometimes you may want to have a custom Login Section for specific users only.
Well, it’s not hard to do that, but I think there a few things to keep in mind!
First of all in Acces Viewer set an item for any “domain\Anonymous” user to not readable. In my case I want to have a download section for registered and public users.

Then create a login page with an Login.ascx user control (will be shown later).
Adjust your web.config in the authentication section to point to your login page:
<authentication mode="Forms">
<forms name=".ASPXAUTH" cookieless="UseCookies" loginUrl="/Login.aspx" timeout="1440" />
</authentication>
If you know try to access an item within our restricted folder you will be redirected to the login page.
Note: It is highly recommended to create a custom domain for users which have access to the restricted item, it is easier to maintenace and you can easily separate them from the default sitecore domain. You can access the users in this domain simply with the Sitecore.Security.Domains.Domain class.
Domain.GetDomain("MyDomain").GetUsers().Where(x => x.Name != "MyDomain\\Anonymous").ToList();
Configure your Domain in App_Config/Security/Domains.config:
<domain name="MyDomain" isDefault="true" ensureAnonymousUser="true" defaultProfileItemId="{16C4FB8F-123A-43A2-98AC-779CAF1B3152}" locallyManaged="true" />
As you can see I configured a default profile item. This may be interestant because we can set additional user informations within our profile! Create a new item in the core database in /sitecore/system/Settings/Security/Profiles/MyDomain User Profile. I want to save a Indicator and a customer number beneath my users, so my template for the profile looks like this:

A user with this profile looks like this:

Easy one! Now let’s have a look at my Login User Control:

To Login to the restricted section I want to check the customer number (one of the fields in our custom profile template) and the password. Additional I want to offer the option to remember the credentials for further visits.
Login.ascx code-behind
public partial class Login : GlassUserControl<LoginModel>
{
protected string _returnURL;
public bool RememberMeIsSet { get; set; }
[Inject]
public IAuthenticationService AuthenticationService { get; set; }
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
RememberMeIsSet = false;
InitControls();
SetReturnUrl();
RedirectWhenIsLoggedIn();
if (IsPostBack)
{
return;
}
SetValuesFromCookie();
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
var master = Page.Master as BaseMaster;
if (master == null)
{
return;
}
master.MainNavigation.Visible = false;
}
private void InitControls()
{
Headline.Controls.Add(new LiteralControl(Editable(model => model.Title)));
Text.Controls.Add(new LiteralControl(Editable(model => model.Text)));
RememberMe.Text = Translator.Translate("Remember me");
SignInButton.Text = Translator.Translate("Login");
CustomerNumberText.Controls.Add(new LiteralControl(Translator.Translate("Customer number")));
PasswordText.Controls.Add(new LiteralControl(Translator.Translate("Password")));
}
protected void SignInButton_OnClick(object sender, EventArgs e)
{
if (!Page.IsValid)
{
return;
}
try
{
var domainUser = string.Format("{0}\\{1}", Sitecore.Context.Domain, CustomerNumber.Text);
var isLoggedIn = AuthenticationService.Login(domainUser, CustomerPassword.Text, RememberMeIsSet);
if (isLoggedIn)
{
if (RememberMe.Checked)
{
SetPersistentCookie(domainUser);
}
WebUtil.Redirect(!string.IsNullOrEmpty(_returnURL) ? _returnURL : "/");
}
else
{
ltlMessage.Text = Translator.Translate("Login failed");
}
}
catch (AuthenticationException)
{
ltlMessage.Text = Translator.Translate("Login failed message");
}
}
private void SetReturnUrl()
{
if (Model == null)
{
_returnURL = "/";
return;
}
var linkUrl = Model.Link.GetLinkUrl(new SitecoreContext());
if (string.IsNullOrEmpty(linkUrl))
{
if (Request.QueryString.HasKey("returnUrl") && !string.IsNullOrEmpty(Request.QueryString["returnUrl"]))
{
_returnURL = Request.QueryString["returnUrl"];
}
}
else
{
_returnURL = linkUrl;
}
}
private void RedirectWhenIsLoggedIn()
{
if (Sitecore.Context.IsLoggedIn)
{
WebUtil.Redirect(string.IsNullOrEmpty(_returnURL) ? "/" : _returnURL);
}
}
private void SetPersistentCookie(string username)
{
FormsAuthentication.SetAuthCookie(username, true);
}
private void SetValuesFromCookie()
{
var cookie = Request.Cookies["username"];
if (cookie == null)
{
CustomerNumber.Text = "";
}
else
{
CustomerNumber.Text = cookie.Value;
RememberMeIsSet = true;
}
}
}
The injected AuthenticationService is quite simple:
public class AuthenticationService : IAuthenticationService
{
public bool Login(string username, string password, bool persistent)
{
return AuthenticationManager.Login(username, password, persistent);
}
}
That’s it!
A short note how I create the users: We import them into sitecore from a CSV-File through a scheduled task (more about scheduled tasks coming soon ;)). So every information required is already set beside the user!
Feel free to contact me if you have any questions.
Happy coding! 😉
Regards Dirk
Like this:
Like Loading...