Introduction
This article describes a simple approach to determining whether or not a logged in user is a member of a group within the context of an Sharepoint web based application. The approach shown is based on the use of Windows based authentication in a web site configured to deny anonymous authentication and to require Windows based authentication and redirect the user according to their permission level. So I need to check in which group the user belongs.
The example shown would be useful on a company intranet site in which it was, for example, necessary to restrict access to certain parts of the available functionality based upon the member's role. For example, you might be working with an application that has administrative and general users; the administrative users might have additional application rights such as the ability to delete records. If one were to check for group membership in the administrator's group, the application can show or hide such functionality by getting the currently logged in user and checking whether or not that user is a group member.
I had to check whether the current user exits or not. As per my requirement, I have created different groups with different privileges in Active Directory(AD) and added multiple users to each group. Then I created multiple Sharepoint Groups in a site and added Active Directory User group in appropriate Sharepoint group as per the permission level.
Here, I have a written a generic class to support this type of functionality.
Technical Steps
- It will get all existing Sharepoint groups from the site collection.
- Loop into Groups and check each member of the group whether the member is a domain user group (as we have added Active domain group as member of sharepoint group) or a Sharepoint user.
- If it is a Sharepoint user, then it checks whether this is the current user? If not, return
false
. - If the user is in the domain user Group, it collects all users under that user group and loops the list to that try to find the current user. If a user is found, it returns
true
, otherwise returns false
.
Code
public class UserManager{
{
bool flag = false;
try {
string strUsername = SPContext.Current.Web.CurrentUser.LoginName;
string webUrl = SPContext.Current.Web.Url;
}
catch (Exception ex)
{
}
return flag;
}
private bool CheckRole(string username, string userrole, string webUrl)
{
if (IsUserInSharePointGroup(webUrl, userrole, username))
return true;
else return false;
}
public bool IsUserInSharePointGroup(string webUrl, string groupName, string username)
{
bool reachedMax = false;
bool userIsInGroup = false;
if (String.IsNullOrEmpty(username) || String.IsNullOrEmpty(groupName))
return false;
SPSecurity.RunWithElevatedPrivileges(delegate {
try {
using (SPSite site = new SPSite(webUrl))
{
using (SPWeb web = site.OpenWeb())
{
SPGroup group = site.RootWeb.SiteGroups[groupName];
string upperCaseUserName = username.ToUpper();
foreach (SPUser user in group.Users)
{
if (!user.IsDomainGroup)
{
if (user.LoginName.ToUpper().Equals(upperCaseUserName))
{
userIsInGroup = true;
return;
}
}
else {
if (IsUserInADGroup(web, user.LoginName,
username, out reachedMax))
{
userIsInGroup = true;
return;
}
}
}
}
}
}
catch (Exception ex)
{
}
});
return userIsInGroup;
}
private static bool IsUserInADGroup(SPWeb web, string groupName,
string username, out bool reachedMax)
{
SPPrincipalInfo[] principals =
SPUtility.GetPrincipalsInGroup(web, groupName, 500, out reachedMax);
if (principals == null || principals.Length == 0)
{
return false;
}
else {
string upperCaseUserName = username.ToUpper();
foreach (SPPrincipalInfo principal in principals)
{
if (!principal.IsSharePointGroup && principal.PrincipalType
!= SPPrincipalType.SecurityGroup &&
principal.DisplayName.ToUpper() != "SYSTEM ACCOUNT")
{
if (principal.LoginName.ToUpper() == upperCaseUserName)
{
return true;
}
}
else if (principal.PrincipalType == SPPrincipalType.SecurityGroup)
{
if (IsUserInADGroup(web, principal.LoginName, username, out reachedMax))
{
return true;
}
}
}
return false;
}
}
}
Here you will only need to call "IsCurrentUserInRole(string SPGroupName)
" method by passing the Sharepoint group name to check whether the current user exists in the specified Sharepoint group and returns true
if user exists, otherwise return false
.
History
- 19th May, 2010: Initial post