Home » AzureRSS

How to create SPUser or SPUserToken from Username and Password (Forms-based Authentication)

Hi all,

I need to connect and work with SharePoint Document Library, while my console application is external and running aside the SharePoint context. The SharePoint applicaiton is running with Forms Based Authentication (FBA).

I think I need to instantiate an impersonated SPSite object, but in order to do that I need SPUserToken object.

Is there any way to create an instance of SPUserToken or SPUser object by providing Username and Password?

Thanks in advance.

 

 

 

 

 

 

5 Answers Found

 

Answer 1

You can't instantiate  an SPSIte object  if you're working outside the sharepoint  context. You have to use web services to communicate with sharepoint from an external  application.

With web services this is how you can pass credentials:

 

System.Net.NetworkCredential MyCredentials = new NetworkCredential(UserName, Password, Domain);

WebService.Lists DevList = new WebService.Lists();
               
DevList.Credentials = MyCredentials ;

 

 

Answer 2

Hi Karla,

 

Thanks for your response.

What I'm actually trying to do is to add a new document  to Document Library in SPSite with FBA.

I didn't find a Web Service method allowing to do that. 

Could you please point me to the relevant one?

 

Thanks.

 

Answer 3

Hi MishaSoft,

 

Do you want to upload a document  or add a new document? If you want to upload a file to the document library, you can user the Cope web Service to achieve it.

Web Service: CopyWeb Reference: http://<Site>/_vti_bin/Copy.asmx

 

More information:

http://msdn.microsoft.com/en-us/library/copy.copy.copyintoitems(office.12).aspx

http://social.technet.microsoft.com/Forums/en-US/sharepointdevelopment/thread/2a6fe1e3-761b-4e74-bc5a-df1cc4c09afc

 

Answer 4

Hi Wane,

Thanks for your reply. It seems I did one step forward.

FieldInformation fi = new FieldInformation();
CopyResult[] res = null;
      
Copy cpy = new Copy();
cpy.Credentials = new NetworkCredential("spadmin", "******", "");
      
uint rc = cpy.CopyIntoItems(
        "http://mysrv/docs/doclib/doc.txt",
        newstring[] { "http://mysrv/docs/doclib/doc.txt" },
        new FieldInformation[] { fi },
        Encoding.UTF8.GetBytes(Convert.ToBase64String(System.IO.File.ReadAllBytes("c:\\doc.txt"))),
        out res);



Now I success to upload the file to server with Windows based  authenitcaiton, but it still doesn't work  for environment with forms  Based authentication. I provide the forms user credentials through Copy.Credentials property, but it throws the following exception:  

The request failed with the error message:
--
<html><head><title>Object moved</title></head><body><h2>Object moved to <ahref="%2f_layouts%2flogin.aspx%3fReturnUrl%3d%252f_vti_bin%252fCopy.asmx">here</a>.</h2></body></html>

It seems the server ignores the credentials and redirects the WS request to login.aspx

Any ideas how I can authenticate the Forms-based user ?

 Thanks.

 

 

Answer 5

Thanks to all for the help.

I've finally figured out the solution.

      //Login
      Authentication auth = new Authentication();
      auth.CookieContainer = new CookieContainer();
      auth.AllowAutoRedirect = true;
      LoginResult lrc = auth.Login ("spadmin", "******");

      //Upload the file
      FieldInformation fi = new FieldInformation();
      CopyResult[] res = null;
      
      Copy cpy = new Copy();
      cpy.CookieContainer = auth.CookieContainer;
      uint rc = cpy.CopyIntoItems(
        "http://mysrv/docs/doclib/doc.txt",
        new string[] { "http://mysrv/docs/doclib/doc.txt" },
        new FieldInformation[] { fi },
        Encoding.UTF8.GetBytes(Convert.ToBase64String(System.IO.File.ReadAllBytes("c:\\doc.txt"))),
        out res);
 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter