SharePoint: How to Authenticate with C #

I have the following piece of code:

using (SPSite site = new SPSite(this.ListAddress))
        {
            using (SPWeb web = site.OpenWeb())
            {

            }

        }

      

How can I authenticate to set the username + password file in the config file.

+2


source to share


3 answers


Make the user you want to run the code known in SharePoint, then using

SPSite.RootWeb.EnsureUser("username").UserToken

you can get this user SPUserToken use this to open SPSite like



var token = SPSite.RootWeb.EnsureUser("usernameToImpersonate").UserToken;

using (SPSite site = new SPSite(token, this.ListAddress))
{
  using (SPWeb web = site.OpenWeb())
  {
    // code here will be executed as selected user
  }
}

      

+9


source


Running the code above will authenticate under the current user ID, that is, the ID of the user running the current thread.

You can run code as a different user using the NetworkCredntial class in conjunction with the WindowsIdentity class

See these two parts:



http://msdn.microsoft.com/en-us/library/system.net.networkcredential%28VS.71%29.aspx

http://msdn.microsoft.com/en-us/library/system.security.principal.windowsidentity.aspx

+2


source


It is not clear what you want to make from the question. However, if you are looking for impersonation, note that you can pass an SPUserToken object to the SPSite constructor .

Any objects that you then create on the impersonated SPSite object will behave as if they were created by that user.

+1


source







All Articles