In this blog, we will create a Login page which will help registered users to login to the site. If you want to see how to create Registration work then you should refer this blog.
The Login process idea for this blog has been taken from Sitecore site.
To design a Login Page, we will need below pieces to be in place:
- Create a Login form with below fields.
a. Username
b. Password
c. Submit button - Create a custom submit action to validate the user
- Add the TryParse method so that your custom submit action is called, this step is very important as the link shared above expects parameters and we are not passing any.
Create a Login Form:

Create 3 Fields (Username – Single Line, Password – Password) and a Submit button and add a custom submit action which we will create in next step.
Create custom Submit Action:
Create a .cs file and add a logic to login a registered extranet user:
using Sitecore;
using Sitecore.Diagnostics;
using Sitecore.ExperienceForms.Models;
using Sitecore.ExperienceForms.Processing;
using Sitecore.ExperienceForms.Processing.Actions;
using Sitecore.Security.Accounts;
using Sitecore.Security.Authentication;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TestApplication.Models;
namespace TestApplication.SubmitAction
{
public class LoginUser : SubmitActionBase<string>
{
public LoginUser(ISubmitActionData submitActionData) : base(submitActionData)
{
}
protected override bool TryParse(string value, out string target)
{
target = string.Empty;
return true;
}
protected override bool Execute(string data, FormSubmitContext formSubmitContext)
{
Assert.ArgumentNotNull(data, nameof(data));
Assert.ArgumentNotNull(formSubmitContext, nameof(formSubmitContext));
var fields = GetFormFields(data, formSubmitContext);
Assert.IsNotNull(fields, nameof(fields));
if (UsernameOrPasswordFieldIsNull(fields))
{
return AbortForm(formSubmitContext);
}
var user = Login(fields.Username, fields.Password);
if (user == null)
{
return AbortForm(formSubmitContext);
}
//Redirect to external URL
var HomePageUrl = $"{HttpContext.Current.Request.Url?.Scheme}://tp101sc.dev.local/corp";
formSubmitContext.RedirectUrl = HomePageUrl;
formSubmitContext.RedirectOnSuccess = true;
return true;
}
protected virtual User Login(string userName, string password)
{
var accountName = string.Empty;
var domain = Context.Domain;
if (domain != null)
{
accountName = domain.GetFullName(userName);
}
var result = AuthenticationManager.Login(accountName, password);
if (!result)
{
return null;
}
var user = AuthenticationManager.GetActiveUser();
return user;
}
private string GetValue(object field)
{
return field?.GetType().GetProperty("Value")?.GetValue(field, null)?.ToString() ?? string.Empty;
}
private LoginUserFormFields GetFormFields(string data, FormSubmitContext formSubmitContext)
{
Assert.ArgumentNotNull(data, nameof(data));
Assert.ArgumentNotNull(formSubmitContext, nameof(formSubmitContext));
return new LoginUserFormFields
{
Username = GetValue(formSubmitContext.Fields.FirstOrDefault(f => f.Name.Equals("Username"))),
Password = GetValue(formSubmitContext.Fields.FirstOrDefault(f => f.Name.Equals("Password")))
};
}
private bool UsernameOrPasswordFieldIsNull(LoginUserFormFields field)
{
Assert.ArgumentNotNull(field, nameof(field));
return field.Username == null || field.Password == null;
}
private bool UsernameOrPasswordValueIsNull(LoginUserFieldValues values)
{
Assert.ArgumentNotNull(values, nameof(values));
return string.IsNullOrEmpty(values.Username) || string.IsNullOrEmpty(values.Password);
}
private bool AbortForm(FormSubmitContext formSubmitContext)
{
formSubmitContext.Abort();
return false;
}
internal class LoginUserFormFields
{
public string Username { get; set; }
public string Password { get; set; }
public LoginUserFieldValues GetFieldValues()
{
return new LoginUserFieldValues
{
Username = FieldHelper.GetValue(Username),
Password = FieldHelper.GetValue(Password)
};
}
}
internal class LoginUserFieldValues
{
public string Username { get; set; }
public string Password { get; set; }
}
}
}
Add submit action under Settings:

Add a Try Parse Empty string in your code (Custom Submit Action) [P.S. Ignore if you have directly copied the above code]:
protected override bool TryParse(string value, out string target)
{
target = string.Empty;
return true;
}
Form submission should redirect the user to Home page:
The Form will look something like this:

Once you submit with a valid credentials, a user will be redirected to the Home page:

If you try to submit the login form with invalid credentials then you will see a Login Failed message:

Hope it helps!! Lets meet in next part to understand how we can set up website as a gated and redirect to Login page for accessing any pages.
Thank you.. Keep Learning.. Keep Sitecoring.. 🙂
Pingback: Creating Simple Authentication Module for Sitecore Site – Part 3 (Gated Website) | Sitecore Diaries