Solution: Simulate Moss users to call WebService to open a personal site for operations

Source: Internet
Author: User

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.