views:

1162

answers:

3

We use Sharepoint as CMS for our webpages at work. I know how to create controls that can be only visible if you have logged in in SharePoint with:

<Sharepoint:SPSecurityTrimmedControl ID="SPSecurityTrimmedControl1" runat="server" PermissionsString="AddAndCustomizePages"><br />
<Sharepoint:CssLink ID="CssLink1" runat="server"/><br />
</Sharepoint:SPSecurityTrimmedControl>

But I want to know how to make controls visible (or whatever) programmatically depending on permissions.

I cannot use the methods for .NET windows form authentication like:

        if (!(HttpContext.Current.User == null) && HttpContext.Current.User.Identity.IsAuthenticated){}

because we use this for anonymous users who has another type of log in.

Could you provide some code? I know that it must be something like verifying the SPContext.Current.FormContext.

+4  A: 

How are the users authenticated? With forms authentication or Windows/active directory?

If active directory, then I think in that case you might need to get a reference to the current SPWeb, and then do web.CurrentUser.ID. This might come out null when you are anonymous. If not, try web.SiteUsers.GetByID(web.CurrentUser.ID) and see what you get.

strongopinions
They are authenticated through Sharepoint administration who is linked to Active Directory. The thing is there is a Sharepoint API to do these things but I have not had the time to look at this part.
netadictos
I think in that case you might need to get a reference to the current SPWeb, and then do web.CurrentUser.ID. This might come out null when you are anonymous. If not, try web.SiteUsers.GetByID(web.CurrentUser.ID) and see what you get.
strongopinions
Could you put your commentary in your answer so that i can vote it? I have finally verified SPContext.Current.Web.CurrentUser is not null
netadictos
A: 

Although I haven't tested it, I imagine the LoginName property of the SPUser object will be blank, or throw an exception.

... of course, its never safe to presume anything when dealing w/the SharePoint OM :(

Jason
+1  A: 

DoesUserHavePermissions

You can use this method on the current web to check if the current user has a specific permission.

I assume your authenticated users have some permission to check for that the anonymous crowd is denied.

Øyvind Skaar