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

    Question

  • 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.

     

     

     

     

     


    Thanks, Michael
    Thursday, July 15, 2010 12:51 PM

Answers

  • 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);
    

    Thanks, Michael
    • Marked as answer by Wayne Fan Monday, July 19, 2010 1:57 AM
    Sunday, July 18, 2010 5:20 PM

All replies

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

     

    Friday, July 16, 2010 7:52 PM
  • 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.


    Thanks, Michael
    Saturday, July 17, 2010 10:01 AM
  • 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

    Sunday, July 18, 2010 12:55 PM
  • 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",
            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);
    
    
    
    

    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 <a href="%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.

     


    Thanks, Michael
    Sunday, July 18, 2010 4:46 PM
  • 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);
    

    Thanks, Michael
    • Marked as answer by Wayne Fan Monday, July 19, 2010 1:57 AM
    Sunday, July 18, 2010 5:20 PM