[RESOLVED] help with credentials
hi guys i have a problem about the credentials will you please help me. When i try to run the code below into a machine that is not logged-In into our domain(where sharepoint resides) it always gives me an error.
Error Detail:
Quote:
System.Net.WebException was unhandled
Message="The request failed with HTTP status 401: Unauthorized."
Source="System.Web.Services"
StackTrace:
at System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage message, WebResponse response, Stream responseStream, Boolean asyncCall)
at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters)
at WindowsApplication1.List_Service.Lists.GetListCollection() in C:\Documents and Settings\daimous\My Documents\Visual Studio 2005\Projects\BCMD\WindowsApplication1\Web References\List_Service\Reference.cs:line 133
at WindowsApplication1.frmindex.button2_Click(Object sender, EventArgs e) in C:\Documents and Settings\daimous\My Documents\Visual Studio 2005\Projects\BCMD\WindowsApplication1\frmindex.cs:line 732
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoCompo nentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at WindowsApplication1.Program.Main() in C:\Documents and Settings\daimous\My Documents\Visual Studio 2005\Projects\BCMD\WindowsApplication1\Program.cs:line 17
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
but when i try to run it into machine that is currently logged-In into our domain it does not have any problem. but i want my windows application to work on both either under the domain or not.
Code:
//List_Service is one of my Web References which is referenced to
//Sharepoint List Service
List_Service.Lists myservice = new List_Service.Lists();
// Authenticate the current user by passing their default
// credentials to the Web Service from the system credential
myservice.Credentials = System.Net.CredentialCache.DefaultCredentials;
// Declare an XmlNode object and initialize it with the XML
// response from the GetListCollection method.
System.Xml.XmlNode node = myservice.GetListCollection();
// Loop through XML response and parse out the value of the
// Title attribute for each list.
foreach (System.Xml.XmlNode xmlnode in node)
{
txtUsername.Text += xmlnode.Attributes["Title"].Value + "\n";
}
any input will be greatly appreciated...thanks in advance!
Re: help with credentials
Your problem is where you set the service credentials (myservice.Credentials).
System.Net.CredentialCache.DefaultCredentials retrieves the currently logged in (i.e. thread principal) account - this account must have rights on SharePoint, and if you're not on the domain, I believe you'll find that (in the Security event log of your SPS server) there are either several failure audits, or an anonymous login.
Hazarding a guess - haven't come across a situation like this in our projects - you may be able to allow anonymous users access to the specific area or web: if you can, would suggest giving them reader rights.
A last resort would be to wrap the SharePoint web-service with one of your own, i.e. a web service calling a web service: your web-service will allow anonymous calls, and will impersonate a configured user to call the SharePoint services.
Re: help with credentials
what if the the account of the User has rights on sharepoint but he/she is not on the domain is it posible for the application to just prompt the user for his username, domain and password and use it instead of the defaultcredentials.
Re: help with credentials
Not sure if it'll work if one is not on the domain, but it's worth a check:
Code:
myservice.Credentials = new NetworkCredential("Username", "Password", "Domain"); // Assuming I remember the order of parameters correctly...
myservice.PreAuthenticate = true;
Re: help with credentials
yah!!! it works!!! your the man!!! thanks a lot bro!
Re: help with credentials
will anybody help me with this..I copied the code below from Microsoft Windows Sharepoint Services SDK but it juts wont work everytime i execute that code can anybody body please tell me whats wrong or is there something i missed in that code..by the way the error in the code below happens in this line
Code:
System.Xml.XmlNode nodeListItems = listService.GetListItems(listName,viewName,query,viewFields,rowLimit,queryOptions);
Code:
// Declare and initialize a variable for the Lists Web Service.
List_Service.Lists listService = new List_Service.Lists();
/* Authenticate the current user by passing their default
credentials to the Web Service from the system credential cache.*/
listService.Credentials = System.Net.CredentialCache.DefaultCredentials;
// Set the Url property of the service for the path to a subsite.
listService.Url = "http://sharepoint/_vti_bin/Lists.asmx";
// Instantiate an XmlDocument object
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
/* Assign values to the string parameters of the GetListItems method,
using GUIDs for the listName and viewName variables*/
string listName = "{17991794-81BB-494F-9910-CFBF1093A7CF}";
string viewName = "{7137FFF8-48FF-4C69-8C76-0E3BBD1EA7F9}";
string rowLimit = "150";
/*Use the CreateElement method of the document object to create elements
for the parameters that use XML*/
System.Xml.XmlElement query = xmlDoc.CreateElement("Query");
System.Xml.XmlElement viewFields = xmlDoc.CreateElement("ViewFields");
System.Xml.XmlElement queryOptions = xmlDoc.CreateElement("QueryOptions");
/*To specify values for the parameter elements (optional), assign CAML fragments
to the InnerXml property of each element*/
query.InnerXml = "<Where><Gt><FieldRef Name=\"ID\" />" +
"<Value Type=\"Counter\">3</Value></Gt></Where>";
viewFields.InnerXml = "<FieldRef Name=\"Title\" />";
queryOptions.InnerXml = "";
/* Declare an XmlNode object and initialize it with the XML response from
the GetListItems method.*/
System.Xml.XmlNode nodeListItems = listService.GetListItems(listName,viewName,query,viewFields,rowLimit,queryOptions);
// Loop through each node in the XML response and display each item.
foreach (System.Xml.XmlNode listItem in nodeListItems)
{
label1.Text += listItem.OuterXml;
}
Error details...
Quote:
System.Web.Services.Protocols.SoapException was unhandled
Message="Exception of type Microsoft.SharePoint.SoapServer.SoapServerException was thrown."
Source="System.Web.Services"
Actor=""
Lang=""
Node=""
Role=""
StackTrace:
at System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage message, WebResponse response, Stream responseStream, Boolean asyncCall)
at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters)
at WindowsApplication1.List_Service.Lists.GetListItems(String listName, String viewName, XmlNode query, XmlNode viewFields, String rowLimit, XmlNode queryOptions) in C:\Documents and Settings\absalvamante\My Documents\Visual Studio 2005\Projects\BCMD\WindowsApplication1\Web References\List_Service\Reference.cs:line 484
at WindowsApplication1.frmindex.button2_Click(Object sender, EventArgs e) in C:\Documents and Settings\absalvamante\My Documents\Visual Studio 2005\Projects\BCMD\WindowsApplication1\frmindex.cs:line 778
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoCompo nentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at WindowsApplication1.Program.Main() in C:\Documents and Settings\absalvamante\My Documents\Visual Studio 2005\Projects\BCMD\WindowsApplication1\Program.cs:line 17
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()