Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.1
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 29th, 2004, 01:29 PM
Authorized User
 
Join Date: Jun 2004
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
Default Web service with SoapHeaders error..

Hi, I am trying to write a small code to secure an exposed web services(service2.asmx) that returns server time by using cookie type authorization on the web service side using custom SOAP Headers that pass authentication credentials to the web service. I do not want to use SSL. If this works I will also encrypt/dcrypt the Soap message.
To achieve this, I wanted to build a client app. that does the followings:
a)Accepts and sends a username and password ( "admin" and "passord" resp.)
b)gets server time
C)checks to see if the user is logged in.
Well, in my application I tried to create 2 text boxes with labels for the userfor the username ansd password, created lable dispaly message and created 3 buttons : Login, Logout, GetTime.
However, when I tried to write valid credentials and hit the LOGIN and GETTIME Button the server does not display the time. Every Time I hit Login and GETTIME Button, I get SORRY BUDDY YOU DO NOT HAVE ACCESS. I think this is something to do with Cookies( I am not sure.)
The LOGOUT Button works well. The whole code is written below.
Can EXPERTS look at it and Give me comment.
Thank you in Advance.

Service2.asmx
------------
<%@ WebService Language="VB" Class="Service2" %>

Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Configuration
Imports System.Web.Services
Imports Microsoft.VisualBasic.ControlChars
Imports System.XML.Serialization
Imports System.Web.Services.Protocols
Imports System.Web.Security
Imports System.Globalization

'1) Create the public class SOAPHeaders

public class SOAPAuthHeader2:Inherits SoapHeader
       public parameter1 As String
       public Parameter2 As String
End Class

public class Service2:Inherits WebService
       public sHeader As SOAPAuthHeader2


'Checks that the username and password are good. If they pass the test then set
'their Session to TRUE

<WebMethod(EnableSession:=True) ,SoapHeader("sHeader",Direction:=SoapHeaderDirecti on.InOut, Required:=True)> public function SignIn2() As boolean
'check the username and the password passed in the SOAPHeader

       If ((sHeader.Parameter1.ToString() = "admin") AND (sHeader.Parameter2.ToString() = "password") ) Then
           'HTTPContext.session("IsLoggedIn") =true
           'HTTPContext.Current.session("IsLoggedIn") = true

            Context.Session("IsLoggedIn") = true
            return true
       Else
           Context.Session("IsLoggedIn") = false
           return false
       End If

    End function

'3)The protected web method
'This simply gets the current local time of the web server but first check to see
'if the users session is set to true by calling CheckLogin2().


     <WebMethod(EnableSession:=True)> public function GetServerTime2() As String

               If (CheckLogin2() = false )
                   Return "Sorry Buddy you are not allowed to have access"
               Else
                  Return System.DateTime.Now.ToString()
               End if

               'If (CheckLogin() )
                ' Return System.DateTime.Now.ToString()
               'Else
                'Return "Sorry Buddy you are not allowed to have access"
               'End if

      End Function

'4) CheckSession()
'This is first trying to see if context.Session("IsLoggedIn") exists
'If so then it checks to see if it set to TRUE. If it is true the function
',which called it, continues running and vise versa. BUT if the Context.Session("IsLoggedIn")
'DOES NOT exists it too stops the function that called it
'It is in the try/catch because if the Context.Session("IsLoggedIn") did not
'exist the system would fall over

        <WebMethod(EnableSession:=true)> public function CheckLogin2() As Boolean

                   Try
                     if (Context.Session("IsLoggedIn") = false)
                          Return false
                     Else
                        Return true
                     End If
                  Catch
                     return false
                  End Try

        End Function

End Class



-------------------------------
The client app. WSSClient3.aspx
-----------------------------
<%@ Page Language="VB" Debug="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="JAYSERVICE2" %>
<%@ Import Namespace="System.Net.CookieContainer" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>

<script runat="server">

private cookies as System.Net.CookieContainer
private oWebService As Service2
private oSoapHdr As SOAPAuthHeader2


public Sub Main()

            'Cursor = Windows.Forms.Cursors.WaitCursor
            'Cursor.Current = Cursors.WaitCursor
            'Cursor = Cursors.WaitCursor
'set the web service cookieContainer property to cookies object

     If (cookies is Nothing ) Then
       cookies = new System.Net.CookieContainer()
       btnLogOut.Enabled = false
     else
       btnLogOut.Enabled = true
     End If

     oWebService.CookieContainer = cookies
           'Cursor = Cursors.Arrow

End Sub

'create a function for the login
private function Login() AS String
    dim oWebService As new JAYSERVICE2.Service2
    dim oSoapHdr As new JAYSERVICE2.SOAPAuthHeader2

    'send the parameters to the proxy header
    oSoapHdr.Parameter1 = txtUsername.Text.ToString()
    oSoapHdr.Parameter2 = txtPwd.Text.ToString()

    'send the soap header values to the web service
    oWebService.SOAPAuthHeader2Value = oSoapHdr
    oWebService.SignIn2()

    ' call the protected web method
    GetServerTime2()
    Return true
End function

private function GetServerTime2() As String
    dim oWebService As new JAYSERVICE2.Service2
    dim oSoapHdr As new JAYSERVICE2.SOAPAuthHeader2
    'now make a call to the protected web service and assign the result
    'to the message label
    lblMessage.Text = oWebService.GetServerTime2()
    Return True
End function

private function LogOut() As String
    dim oWebService As new JAYSERVICE2.Service2
    dim oSoapHdr As new JAYSERVICE2.SOAPAuthHeader2

    'kill the Cookie
    Cookies = Nothing
    oWebService.CookieContainer() = Nothing

    btnLogOut.Enabled = false
    lblMessage.Text = "Logged Out"
     Return True
End function

' lastly assign the buttons to their functions

sub btnLogIn_Click(Sender as Object, e as EventArgs)
      Login()
End Sub

sub btnLogOut_Click(Sender as Object, e as EventArgs)
     LogOut()
End Sub


sub gettime_Click(Sender as Object, e as EventArgs)
    GetServerTime2()
End Sub

</script>

<html><body>
      <form runat="server">
            Username:
            <asp:TextBox id="txtUsername" runat="server"/><br>
            Password:
            <asp:TextBox id="txtPwd" runat="server" Textmode="password"/><p>
            <asp:Button id="btnlogin" runat="server" Text="Login" onClick="btnLogIn_Click" />
            <asp:Button id="btnLogOut" runat="server" Text="Logout" onClick="btnLogOut_Click" />
            <asp:Button id="btgettime" runat="server" Text="GetTime" onClick="gettime_Click"/><p>
            <asp:Label id="lblMessage" font-size="20pt" runat="server" />
       </form>
</body>
</html>








 
Old July 2nd, 2004, 02:53 PM
Authorized User
 
Join Date: Jun 2004
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It is Ok,
it is working now






Similar Threads
Thread Thread Starter Forum Replies Last Post
Web service call error Tomi J2EE 0 February 1st, 2008 06:22 AM
Web Service Error aliirfan84 .NET Web Services 0 February 14th, 2007 01:01 AM
Error to Add Web Reference from a Web service jdjbarrios ASP.NET 2.0 Professional 0 July 18th, 2006 02:58 PM
web service error redwood_lin Classic ASP Basics 6 January 6th, 2006 07:06 AM
Web service Compilation error belete Classic ASP Databases 1 June 28th, 2004 10:23 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.