Friday, 23 September 2011

Programmatically verify if the current user is anonymous in Sharepoint


If you need to check that the current user is anonymous and does not already exist in your AD you can do so by checking his SPUser.LoginName.

Here is a little sample

public static bool IsAnnonymous(this SPUser user)
{
return user == null || String.IsNullOrEmpty(user.LoginName);
}

public static void IsUserAnnonymous(this SPUser user)
{

if (user.IsAnnonymous())
{
// In Case the user is annonymous then redirect to AccessDenied.aspx Page

response.Redirect("/_layouts/AccessDenied.aspx", true);

}
}
You can call the method by
IsUserAnnonymous(SPContext.web.Current.User);

Ads by Google

No comments:

Post a Comment

Post a Comment