Requirement Description: the whole site can be accessed anonymously. A subsite (professional research) must be visible to internal staff after login, But anonymous access is invisible.
Normal Solution: Set "professional research"'s Anonymous Access to none, that is, anonymous access is not allowed. Add the user group "viewitem" and set the corresponding permissions (read-only ); then, add all employees in the company to this user group to solve the problem.
Reality: there are thousands of employees in the company, including offices all over the country. I feel that there are too many users and it is not easy to add them. I am also worried that I need to do this once if I have other permissions in the future. I am very tired.
Current solution: viewitem is set up for the user group, encoding control, and viewitem user groups added to the "professional research" subsite after users log on
// Add assemblyinfo. CS
Using system. Security;
[Assembly: allowpartiallytrustedcallers ()]
Using system;
Using system. text;
Using system. Web;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using Microsoft. SharePoint;
Using Microsoft. Sharepoint. webcontrols;
Using system. Web. UI. htmlcontrols;
Using Microsoft. Sharepoint. Navigation;
Namespace myanonycheck
{
Public class myanonycheck: webpart, idisposable
{
Private spuser user = NULL;
Private string _ webname = string. empty;
Private string _ groupname = "viewitem ";
Protected override void onload (eventargs E)
{
Base. onload (E );
}
Protected override void render (htmltextwriter writer)
{
Ensurechildcontrols ();
Base. Render (writer );
}
Protected override void createchildcontrols ()
{
Init ();
Base. createchildcontrols ();
}
Public override void dispose ()
{
Base. Dispose ();
}
Private void Init ()
{
Try
{
String siteurl = spcontext. Current. Site. url;
String username = string. empty;
If (_ webname = string. Empty)
{
_ Webname = spcontext. Current. Web. Name;
}
User = spcontext. Current. Site. allwebs [_ webname]. currentuser;
Username = user. loginname;
// Process non-Anonymous Logon
If (user! = NULL)
{
// Permission Improvement
Spsecurity. runwithelevatedprivileges (delegate
{
Using (spsite osite = new spsite (siteurl ))
{
Bool flg = false;
Spweb oweb = osite. openweb (_ webname );
Oweb. allowunsafeupdates = true;
// Obtain user groups and users
Spgroup groups = oweb. sitegroups [_ groupname];
For (INT I = 0; I <groups. Users. Count; I ++)
{
If (groups. Users [I]. loginname. Equals (username ))
{
Flg = true;
Break;
}
}
If (! Flg)
{
Groups. adduser (User );
Groups. Update ();
}
}
});
}
}
Catch (exception E)
{
}
}
[Webbrowsable (true), personalizable (true ),
Webdescription ("Name of the website to be accessed"), webdisplayname ("website name. Example: News/web1 [News] ")]
Public String webname
{
Get {return _ webname ;}
Set {_ webname = value ;}
}
[Webbrowsable (true), personalizable (true ),
Webdescription ("user group name"), webdisplayname ("permission user group")]
Public String groupname
{
Get {return _ groupname ;}
Set {_ groupname = value ;}
}
}
}