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.

3 Answers
3

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.

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 ShakibNov 5 '12 at 20:37

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.)

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 ShakibMar 28 '12 at 8:47