Logo 
Search:

Sharepoint Forum

Ask Question   UnAnswered
Home » Forum » Sharepoint       RSS Feeds

Custom web part problems with security settings

  Asked By: Claude    Date: Jun 19    Category: Sharepoint    Views: 849

i am creating a web part (for a public site) that needs to first see
if you are a site admin or not, and then it will continue to execute
if you are a site admin, else it will show nothing. So, i get the
current
user information, and then check to see if person is admin such as
in this code:


SPWeb rootWeb = SPControl.GetContextSite(Context).RootWeb;
SPRole spRole = rootWeb.Roles["Administrator"];
SPUser currentUser = rootWeb.CurrentUser;

foreach(SPUser roleUser in spRole.Users)
{
if(roleUser.ID.Equals(currentUser.ID))
{
inRole= true;
break;
}
}

This works fine for an authenticated user, but this webpart won't
load when user is coming to
my site via annonymous access. It prompts you to log in.
Is there not a way to access the SP Object model if the user loading
site is anon access? I also tried to install wp into
GAC, and this still didn't help.
I also noticed that if you have the site set as a trusted site in
IE, then this will load ok, but our typical site visitor won't have
this set since we are using SP for our company website.

Share: 

 

2 Answers Found

 
Answer #1    Answered By: Irvin Foley     Answered On: Jun 19

You can check  the HttpContext (System.Web.HttpContext.Current) and get the logon name of the user. If that is blank they are anonymous.

 
Answer #2    Answered By: Deonte Stein     Answered On: Jun 19

The key is the CatchAccessDeniedException property:

Try
' we don't want SharePoint to prompt for credentials...
Dim site  As SPSite = SPControl.GetContextSite(context)
site.CatchAccessDeniedException = False
' get the current SharePoint web  that is the root of the search
Dim web As SPWeb = SPControl.GetContextWeb(Context)
' do something with the web
Catch ex As Exception
user  does not  have permissions. Just eat exception
End Try

 
Didn't find what you were looking for? Find more on Custom web part problems with security settings Or get search suggestion and latest updates.




Tagged: