Started to think you want to build
Windowsidentity = windowsidentity. getcurrent ();
Genericprincipal = new genericprincipal (genericidentity, roles );
Set httpcontext. Current. User to the one constructed above. However, an error occurred while opening the personal site.
Then modify the personal website c: \ Program Files \ common files \ microsoft shared \ Web Server Extensions \ 12 \ template \ sitetemplates \ spspers \ default. aspx
Join
Username: <% = httpcontext. Current. User. Identity. name %>:
Authen: <% = httpcontext. Current. User. Identity. authenticationtype %>
Authe: <% = httpcontext. Current. User. GetType (). tostring () %>
Identity: <% = httpcontext. Current. User. Identity. GetType (). tostring () %>
Output of the "run discovery" Page
Helloworld: sunsheng: Authen: Forms authe: system. Web. Security. roleprincipal identity: system. Web. Security. formsidentity
Only to know the user construction error, should be constructed
System. Web. Security. roleprincipal
System. Web. Security. formsidentity
So I wrote the class
Public class mossfbaimpersonate
{
// Code sample by Ric
// ==========================
// Mossfbaimpersonate = new mossfbaimpersonate (configurationmanager. configurettings. Get ("roleprovidername "));
// Fbaip. Impersonate ([roleprovidernamefromwebconfig], [impersonatingusername]);
/// To do you work here ....
// Fbaip. revoke ();
// ==========================
# Region Public Functions
Public mossfbaimpersonate (string roleprovidername)
{
M_roleprincipal = (iprincipal) httpcontext. Current. user;
M_roleprovider = roleprovidername;
If (null = m_roleprovider)
Throw (new exception ("mossfbaimpersonate: roleprovidername is null! "));
}
Public void impersonate (string username)
{
Httpcontext. Current. User = This. createhttpuser (m_roleprovider, username );
}
Public void revoke ()
{
If (null! = M_roleprincipal)
Httpcontext. Current. User = (iprincipal) m_roleprincipal;
}
# Endregion
# Region private region
Private object m_roleprincipal = NULL;
Private string m_roleprovider = NULL;
Private roleprincipal createhttpuser (string roleprovidername, string username)
{
Genericidentity = new genericidentity (username, "forms ");
Return new roleprincipal (roleprovidername, genericidentity );
}
# Endregion
}
To simulate users.
The code for getting a personal website can be as follows:
Public spweb getpersonalweb (string straccount, string strpersonalsitehost)
{
Spweb myweb = NULL;
Spsite sppersonalsite = NULL;
Mossfbaimpersonate = new mossfbaimpersonate (configurationmanager. deleetworkflow. Get ("roleprovidername "));
Try
{
Mossfbaimpersonate. Impersonate (straccount );
Sppersonalsite = new spsite (strpersonalsitehost );
Myweb = sppersonalsite. rootweb;
Mossfbaimpersonate. revoke ();
}
Catch (system. Threading. threadabortexception thex)
{
Throw thex;
}
Catch (exception ex)
{
Throw ex;
}
Finally
{
// Myweb. Dispose (); // which will be disposed by outside caller.
Sppersonalsite. Dispose ();
}
Return myweb;
}