Link to home
Start Free TrialLog in
Avatar of rowmark
rowmark

asked on

Logging in using UsernameToken using WSE 3.0

Hello,

I have added a reference to the asmx file given by the our 3rd party vendor. On buttonclick I am trying to login to the tool with the code below

Whenever I try to run the app and provide the username,pwd and hit on the login buttn I am getting this error

System.NullReferenceException: Object reference not set to an instance of an object.

at this Line "UsernameToken userTok = new UsernameToken(userName, pwd, Microsoft.Web.Services3.Security.Tokens.PasswordOption.SendPlainText);"

When I build the solution it says build succeeded.

Please help

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Security.Permissions;
using Microsoft.Web.Services3.Security.Tokens;
using Testing;


public partial class _Default : System.Web.UI.Page
{
    private static Testing.InterfaceWse binding;
   



    protected void Page_Load(object sender, EventArgs e)
    {

    }    
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        SecurityContextToken sct = null;
        String userName = txtUserName.Text;
        String pwd = txtPassword.Text;

        if (userName.Length == 0 || pwd.Length == 0)
        {
            Response.Write("Please specify username and password.");
            return;
        }

        // Create a new instance of the web service proxy class
        binding = new Testing.InterfaceWse();

        // Create the UsernameToken with your username and password

        UsernameToken userTok = new UsernameToken("test", "test", Microsoft.Web.Services3.Security.Tokens.PasswordOption.SendPlainText);
       
        binding.RequestSoapContext.Security.Tokens.Add(userTok);
       
        // Call the Login method
        Testing.LoginCredentials loginCreds = binding.Login();
        // Verify that the login was successful
        if (loginCreds.CurrentLoginStatus != Testing.LoginStatus.Success)
        {
            // Display to user why login failed
            Response.Write (loginCreds.CurrentLoginStatus.ToString());
            return;
        }

        // Login was successful, now grab the SecurityContextToken returned
        // Loop through each of the tokens in  response
        foreach (SecurityToken tok in binding.ResponseSoapContext.Security.Tokens)
        {
            // Get the SecurityContextToken that was returned
            if (tok is SecurityContextToken)
                sct = tok as SecurityContextToken;
        }
        // Remove any tokens from the request
        binding.RequestSoapContext.Security.Tokens.Clear();
        // Add the SecurityContextToken
        binding.RequestSoapContext.Security.Tokens.Add(sct);

        return;
       
    }
   
}
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rowmark
rowmark

ASKER

yes