Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

When you are accessing SharePoint site anonymously, you can easily click on the Login link and based on your authentication type either you are prompted for credentials or will be redirected to the login page(if FBA authentication).

Let's say I have hardcoded the credentials in my code and want to do the same programmatically.

If I want to start from the beginning:
One of the SharePoint weakness is that you cant act for anonymous users, for instance you cant change or remove the permissions of a ListItem.
So I concluded that we have to use a user with as limited access as possible like Guest and when anonymous users in some specific page which has my Webpart in it, I hardcode credentials to login and redirect them to a page.

I know about impersonation and other stuff like it which is use in code behind world and it's not what i meant.

So the question is very simple, How Can I Do That?

share|improve this question
    
If you break inheritance of permissions to a list that doesn't remove anonymous access to it, unless you remove it. –  Per Jakobsen Mar 26 '12 at 6:33
    
yes you right about that. but still you can't change or set permissions on either list or the site. i fix the question, thanks. –  Jalali Shakib Mar 26 '12 at 7:37

3 Answers 3

First, you can change the permissions of the anonymous user. The AnonymousPermMask64 property of the SPWeb/SPList object controls what permissions the anonymous user has.

Second, if you want to programmatically log in to a site, you can do so with the System.Web.HttpWebRequest class. You will need a host to run your code, such as a WinForm or console application. If you wish to have code run in the browser and log in, you may run into some difficulties. (Most browsers run with the security credentials of the logged-in user.)

share|improve this answer
1  
yeah thats right. but actually still you cant set permissions for SPListItem. i accurated my question. –  Jalali Shakib Mar 27 '12 at 5:41

As you have hard coded your credentials for anonymous user, assign this Guest User to Readers Group so that he has permission for all List Items.

share|improve this answer
1  
i ain't think i follow you, please be more specific. i haven't the guest user already and how can i relate the anonymous users to Guest user? i mean how sharepoint recognize the anonymous users as guest user? –  Jalali Shakib Mar 28 '12 at 8:47

I believe that this "weakness" it is not SharePoint's and it is not a weakness. Is actually ASP.NET Membership model or classic Windows Authentication. Then you could always to create code to impersonate other accounts for specific functions only, to avoid internet bots performing changes anonymously, e.g. anonymous comment on blog entries.

To change programatically the AnonymousPermissionsMask use this

using System.Web.Configuration;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

using (SPSite specifiedSite = new SPSite("You specified web url"))
{
    using (SPWeb specifiedWeb = specifiedSite.OpenWeb())
    {
        // Enable anonymous access on web application
        SPUrlZone urlZone = SPUrlZone.Default;
        SPWebApplication specifiedWebApplication = specifiedSite.WebApplication;
        SPIisSettings iisSettings = specifiedWebApplication.IisSettings[urlZone];
        iisSettings.AuthenticationMode = AuthenticationMode.Windows;
        iisSettings.AllowAnonymous = true;
        specifiedWebApplication.Update();

        // Enable anonymous access on website
        specifiedWeb.AnonymousState = SPWeb.WebAnonymousState.On;
        specifiedWeb.AnonymousPermMask64 = SPBasePermissions.Open |
            SPBasePermissions.ViewPages | SPBasePermissions.ViewListItems;
        specifiedWeb.Update();

        // Enable anonymous acces on list
        SPList specifiedList = specifiedWeb.GetList("You specified list url");
        specifiedList.AnonymousPermMask64 = SPBasePermissions.ViewListItems |
            SPBasePermissions.AddListItems | SPBasePermissions.EditListItems |
            SPBasePermissions.DeleteListItems;
        specifiedList.Update();
    }
}
share|improve this answer
    
thanks useful piece of code. but it's not my answer. i knew that im able to change the permissions of a list for anonymous users but still not possible for list items. so that's why i decide to have a user on behalf of anonymous users and plan for that user on list items. but still the question remains??? –  Jalali Shakib Nov 5 '12 at 20:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.