Search code examples
formsperlauthenticationmechanizelwp

Form authentication for . NET sites using Perl


We are new in Perl scripting and we wanted to pass through a .NET site authentication. We have an existing code for authentication specific for Sharepoint sites, code snippet below:

my $ua = new LWP::UserAgent('keep_alive' => '1');
print "$url \n";
$ua->credentials("$uname", "$pwd");
my $response =$ua->get($url);
my $cde = $response->code();

We wanted to customize the code so that it will work specific for .NET sites authentication. We used WWW::Mechanize, see code snippet below:

my $mech = WWW::Mechanize->new();
$mech->cookie_jar(HTTP::Cookies->new());
$mech->get($url);
$mech->form_name('Login');
$mech->field("UserName", $username);
$mech->field("Password", $password);
$mech->click();

But when the program is run, an error is shown: "There is no form named 'Login' in sample.pl". Any thoughts on how to authenticate forms specific for .NET sites?

Adding: HTML form code snippet...

<div id="loginArea">        
        <div id="loginMessage" class="groupMargin">Sign in with your account</div>
        <form method="post" id="Login" autocomplete="off" novalidate="novalidate" onkeypress="if (event &amp;&amp; event.keyCode
== 13) Login.submitLoginRequest();" action="">
            <div id="formsAuthenticationArea">
                <div id="userNameArea">
                    <input id="userNameInput" name="UserName" type="email" value="" tabindex="1" class="text fullWidth" spellcheck="false" placeholder="Username" autocomplete="off">     
                </div>
                <div id="passwordArea">
                     <input id="passwordInput" name="Password" type="password" tabindex="2" class="text fullWidth" placeholder="Password" autocomplete="off">                            

                </div>
                <div id="kmsiArea" style="display:none">
                    <input type="checkbox" name="Kmsi" id="kmsiInput" value="true" tabindex="3">
                    <label for="kmsiInput">Keep me signed in</label>
                </div>
                <div id="submissionArea" class="submitMargin">
                    <span id="submitButton" class="submit" tabindex="4" onkeypress="if (event &amp;&amp; event.keyCode == 32) Login.submitLoginRequest();" onclick="return Login.submitLoginRequest();">Sign in</span>
                </div>
            </div>
        </form>
    </div>

Solution

  • Usually this code works for .NET sites:

    $mech->submit_form(
        with_fields => {
            login => "somelogin",
            pass  => "somepass",
        },
        button => "submit_button_name",
    );
    

    But you have weird error message: you're selecting "Login" form but $mech searching for "loginForm"... Can you show actual form's HTML?

    Also there is no need to do $mech->cookie_jar(...) because $mech do this by default.

    UPDATE Try this code (Login is id of your form but not the name):

    $mech->submit_form(
        with_fields => {
            UserName => "somelogin",
            Password  => "somepass",
        },
    );