HTTP status 401: Unauthorized when accessing web service method via winform in same VS2005 solution.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • hazz

    HTTP status 401: Unauthorized when accessing web service method via winform in same VS2005 solution.

    I don't get it. I have a VS2005 solution with a web service project and a
    windows project. The web service when tested on its own works fine. But when
    I add it as a web reference and refer to it my windows form code behind I
    receive the following;
    [System.Net.WebE xception] {"The request failed with HTTP status 401:
    Unauthorized."}
    when trying to execute
    this.dataGridVi ew1.DataSource = ws.ReturnStuden tDS();

    not sure if it is relevant but the web.config in the web service project has
    <authenticati on mode="Windows"/>


    Here is the webservice.
    [WebService(Name space = "http://tempuri.org/")]
    [WebServiceBindi ng(ConformsTo = WsiProfiles.Bas icProfile1_1)]
    public class Service : System.Web.Serv ices.WebService
    {
    public Service () {

    }
    [WebMethod]
    public DataSet ReturnStudentDS () {
    SqlConnection myConnection = new
    SqlConnection(" server=(local); database=Smartd b ;Trusted_Connec tion=yes");
    SqlDataAdapter myCommand = new SqlDataAdapter( "select * from
    Student", myConnection);
    DataSet ds = new DataSet();
    return ds;
    }
    }


    Here is the winform accessing the webmethod.
    public Form1()
    {
    InitializeCompo nent();
    try
    {

    returnStudentDS .Service ws = new returnStudentDS .Service();
    this.dataGridVi ew1.DataSource = ws.ReturnStuden tDS();
    <-------------------- line where exception is thrown
    }
    catch (Exception e)

    {
    Console.WriteLi ne("{0} Exception caught.", e);

    }

    Thanks,
    -hazz


  • Alvin Bruney - ASP.NET MVP

    #2
    Re: HTTP status 401: Unauthorized when accessing web service method via winform in same VS2005 solution.

    is the user executing the code authenticated?
    It seems like it is not. You can try removing authentication and seeing if
    you can hit the webservice.

    --
    Regards,
    Alvin Bruney [MVP ASP.NET]

    [Shameless Author plug]
    The Microsoft Office Web Components Black Book with .NET
    Now Available @ www.lulu.com/owc
    Forth-coming VSTO.NET - Wrox/Wiley 2006
    -------------------------------------------------------



    "hazz" <hazz@sonic_net > wrote in message
    news:evzwdFI5FH A.696@TK2MSFTNG P09.phx.gbl...[color=blue]
    > I don't get it. I have a VS2005 solution with a web service project and a
    > windows project. The web service when tested on its own works fine. But[/color]
    when[color=blue]
    > I add it as a web reference and refer to it my windows form code behind I
    > receive the following;
    > [System.Net.WebE xception] {"The request failed with HTTP status 401:
    > Unauthorized."}
    > when trying to execute
    > this.dataGridVi ew1.DataSource = ws.ReturnStuden tDS();
    >
    > not sure if it is relevant but the web.config in the web service project[/color]
    has[color=blue]
    > <authenticati on mode="Windows"/>
    >
    >
    > Here is the webservice.
    > [WebService(Name space = "http://tempuri.org/")]
    > [WebServiceBindi ng(ConformsTo = WsiProfiles.Bas icProfile1_1)]
    > public class Service : System.Web.Serv ices.WebService
    > {
    > public Service () {
    >
    > }
    > [WebMethod]
    > public DataSet ReturnStudentDS () {
    > SqlConnection myConnection = new
    > SqlConnection(" server=(local); database=Smartd b ;Trusted_Connec tion=yes");
    > SqlDataAdapter myCommand = new SqlDataAdapter( "select * from
    > Student", myConnection);
    > DataSet ds = new DataSet();
    > return ds;
    > }
    > }
    >
    >
    > Here is the winform accessing the webmethod.
    > public Form1()
    > {
    > InitializeCompo nent();
    > try
    > {
    >
    > returnStudentDS .Service ws = new[/color]
    returnStudentDS .Service();[color=blue]
    > this.dataGridVi ew1.DataSource = ws.ReturnStuden tDS();
    > <-------------------- line where exception is thrown
    > }
    > catch (Exception e)
    >
    > {
    > Console.WriteLi ne("{0} Exception caught.", e);
    >
    > }
    >
    > Thanks,
    > -hazz
    >
    >[/color]


    Comment

    • hazz

      #3
      Re: HTTP status 401: Unauthorized when accessing web service method via winform in same VS2005 solution.

      I am running it all on my local machine Alvin.
      Stupid question on my part. What do you mean executing the code
      authenticated. How do I remove authentication?
      setting it to None in the web config? I tried that.

      Thanks, -hazz

      "Alvin Bruney - ASP.NET MVP" <www.lulu.com/owc> wrote in message
      news:u96Y1cJ5FH A.2396@TK2MSFTN GP14.phx.gbl...[color=blue]
      > is the user executing the code authenticated?
      > It seems like it is not. You can try removing authentication and seeing if
      > you can hit the webservice.
      >
      > --
      > Regards,
      > Alvin Bruney [MVP ASP.NET]
      >
      > [Shameless Author plug]
      > The Microsoft Office Web Components Black Book with .NET
      > Now Available @ www.lulu.com/owc
      > Forth-coming VSTO.NET - Wrox/Wiley 2006
      > -------------------------------------------------------
      >
      >
      >
      > "hazz" <hazz@sonic_net > wrote in message
      > news:evzwdFI5FH A.696@TK2MSFTNG P09.phx.gbl...[color=green]
      >> I don't get it. I have a VS2005 solution with a web service project and a
      >> windows project. The web service when tested on its own works fine. But[/color]
      > when[color=green]
      >> I add it as a web reference and refer to it my windows form code behind I
      >> receive the following;
      >> [System.Net.WebE xception] {"The request failed with HTTP status 401:
      >> Unauthorized."}
      >> when trying to execute
      >> this.dataGridVi ew1.DataSource = ws.ReturnStuden tDS();
      >>
      >> not sure if it is relevant but the web.config in the web service project[/color]
      > has[color=green]
      >> <authenticati on mode="Windows"/>
      >>
      >>
      >> Here is the webservice.
      >> [WebService(Name space = "http://tempuri.org/")]
      >> [WebServiceBindi ng(ConformsTo = WsiProfiles.Bas icProfile1_1)]
      >> public class Service : System.Web.Serv ices.WebService
      >> {
      >> public Service () {
      >>
      >> }
      >> [WebMethod]
      >> public DataSet ReturnStudentDS () {
      >> SqlConnection myConnection = new
      >> SqlConnection(" server=(local); database=Smartd b ;Trusted_Connec tion=yes");
      >> SqlDataAdapter myCommand = new SqlDataAdapter( "select * from
      >> Student", myConnection);
      >> DataSet ds = new DataSet();
      >> return ds;
      >> }
      >> }
      >>
      >>
      >> Here is the winform accessing the webmethod.
      >> public Form1()
      >> {
      >> InitializeCompo nent();
      >> try
      >> {
      >>
      >> returnStudentDS .Service ws = new[/color]
      > returnStudentDS .Service();[color=green]
      >> this.dataGridVi ew1.DataSource = ws.ReturnStuden tDS();
      >> <-------------------- line where exception is thrown
      >> }
      >> catch (Exception e)
      >>
      >> {
      >> Console.WriteLi ne("{0} Exception caught.", e);
      >>
      >> }
      >>
      >> Thanks,
      >> -hazz
      >>
      >>[/color]
      >
      >[/color]


      Comment

      Working...