join1Below is a core list of commonly used authentication and authorization methods to act like a quick reference guide for people building membership based Sitecore solutions.

This reference is intended to get you on the right track immediately from the one source.

If anyone has anything they would like me to add to the list please comment below and I’ll update it accordingly.

Using

These are the required references for the following examples:

using System;
using System.Collections.Generic;
using System.Linq;
using Sitecore;
using Sitecore.Security;
using Sitecore.Security.Accounts;
using System.Web.Security;
using Sitecore.Security.Authentication;

Retrieving Users

//Get all users regardless of domain, Sitecore, extranet, custom etc:
public IEnumerable<User> GetUsers()
{
return UserManager.GetUsers();
}

//Get all users for a particular domain, in most situations this will be of more use then getting all users:
public IEnumerable<User> GetUsers(string domainName)
{
return UserManager.GetUsers().Where(user => user.Domain != null && user.Domain.Name == domainName);
}

//Get a user from a custom property. This can be useful to find a user by some unique value.
public User GetUserFromCustomField(string fieldName, string fieldValue)
{
return UserManager.GetUsers().FirstOrDefault(user => user.Profile.GetCustomProperty(fieldName) == fieldValue);
}

//Get a collection of users from a custom property. An example might be to get all male users.
public IEnumerable<User> GetUsersFromCustomField(string fieldName, string fieldValue)
{
return UserManager.GetUsers().Where(user => user.Profile.GetCustomProperty(fieldName) == fieldValue);
}

//Again with queries like this it is most likely that filtering by domain name will be more useful then searching all users.
public User GetUserFromCustomField(string fieldName, string fieldValue, string domainName)
{
return UserManager.GetUsers().FirstOrDefault(user => user.Profile.GetCustomProperty(fieldName) == fieldValue && user.Domain.Name == domainName);
}

public IEnumerable<User> GetUsersFromCustomField(string fieldName, string fieldValue, string domainName)
{
return UserManager.GetUsers().Where(user => user.Profile.GetCustomProperty(fieldName) == fieldValue && user.Domain.Name == domainName);
}

//Get user by user name, it must include the domain.
public User GetUserByUserName(string domainName, string userName)
{
return User.FromName(String.Format(@"{0}\{1}", domainName, userName), false);
}

//Get user by email, first get the username for the email then get user by username.
public User GetUserByEmail(string domainName, string email)
{
var userName = Membership.GetUserNameByEmail(email);
return !String.IsNullOrEmpty(userName) ? GetUserByUserName(userName, domainName) : null;
}

//Check if a user exists
public bool DoesUserExist(string domainName, string userName)
{
return User.Exists(String.Format(@"{0}\{1}", domainName, userName));
}

//Get the current context user. This will by default be extranet\Anonymous if no one is logged in.
public User GetCurrentUser()
{
return Context.User;
}

Users Roles

public  IEnumerable<Role> GetUsersRoles(UserProfile profile)
{
return profile.ProfileUser.Roles;
}

public void AddRoleToUser(UserProfile profile, string roleName, string domainName)
{
profile.ProfileUser.Roles.Add(RoleHelper.GetRole(domainName, roleName));
profile.Save();
}

public void RemoveRoleFromUser(UserProfile profile, string roleName, string domainName)
{
profile.ProfileUser.Roles.Remove(RoleHelper.GetRole(domainName, roleName));
profile.Save();
}

public bool IsUserInRole(User user, string role)
{
return user != null && user.IsInRole(role);
}

User CRUD

public void AddUser(string domainName, string userName, string password, string email)
{
Membership.CreateUser(String.Format(@"{0}\{1}", domainName, userName), password, email);
}

public string GetUsersCutomProperty(User user, string fieldName)
{
return user.Profile.GetCustomProperty(fieldName);
}

//Update the Users default sitecore and custom fields
public void UpdateUser(UserProfile profile, string email, string fullName, string customFieldName, string customFieldValue)
{
profile.Email = email;
profile.FullName = fullName;
profile.SetCustomProperty(customFieldName, customFieldValue);
profile.Save();
}

public void DeleteUser(User user)
{
user.Delete();
}

Is authenticated, is administrator

//Method not required but here for completeness
public bool IsUserAuthenticated(User user)
{
return user.IsAuthenticated;
}

//Method not required but here for completeness
public bool IsUserAdministrator(User user)
{
return user.IsAdministrator;
}

Change and reset users password

public void ChangeUsersPassword(string userName, string currentPassword, string newPassword)
{
Membership.Provider.ChangePassword(userName, currentPassword, newPassword);
}

public static string ResetUsersPassword(string userName)
{
var membershipUser = Membership.GetUser(userName);
return membershipUser != null ? membershipUser.ResetPassword() : string.Empty;
}

Authentication

//login without password
public void LoginUser(string domainName, string userName)
{
AuthenticationManager.Login(string.Format(@"{0}\{1}", domainName, userName), false);
}

//default login
public void LoginUser(string domainName, string userName, string password)
{
AuthenticationManager.Login(string.Format(@"{0}\{1}", domainName, userName), password, false);
}

public void LogOutUser()
{
AuthenticationManager.Logout();
}

Roles

//Roles are seperate per domain
public Role GetRole(string domainName, string roleName)
{
return Role.FromName(string.Format(@"{0}\{1}", domainName, roleName));
}

public IEnumerable<string> GetRoles()
{
return Roles.GetAllRoles();
}

public IEnumerable<string> GetRoles(string domainName)
{
return Roles.GetAllRoles().Where(r => r.Contains(domainName));
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s