Form authentication for . NET sites using Perl - forms

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 && 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 && event.keyCode == 32) Login.submitLoginRequest();" onclick="return Login.submitLoginRequest();">Sign in</span>
</div>
</div>
</form>
</div>

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",
},
);

Related

How to redirect back to the page after redirect from that page to login with flask login manager with custom login.html

So i made a login form:
<form action="{{ url_for('login', next=request.endpoint) }}" method="post" class="card-body cardbody-color p-lg-5">
<div class="mb-3">
<input type="text" class="form-control" name="login" placeholder="login" required>
</div>
<div class="mb-3">
<input type="password" class="form-control" name="password" placeholder="password" required>
</div>
<div class="text-center">
<button type="submit" class="btn btn-success px-5 mb-5 w-100">log in</button>
</div>
</form>
key point is:
action="{{ url_for('login', next=request.endpoint) }}"
Thats my login backend:
#server.route("/login", methods=['GET', 'POST'])
def login():
username = request.form.get('login')
password = request.form.get('password')
next_page = request.args.get('next')
if not next_page:
next_page = '/'
if current_user.is_authenticated:
flash('You are already logged in!')
return redirect(url_for('profile'))
if username and password:
user = User.query.filter_by(username=username).first()
if not user:
flash('incorrect login')
return render_template('flask_user/login.html')
if check_password_hash(user.password, password):
login_user(user, remember=True)
return redirect(next_page)
else:
flash('incorrect password')
else:
flash('Enter login and password')
return render_template('flask_user/login.html')
That's my redirect handler:
#login_manager.unauthorized_handler
def redirect_to_signin(response):
return redirect(url_for('login'), next=request.endpoint)
Login manager works perfectly:
http://10.104.4.95:8080/login?next=/arm_analyst
But after i am pressing the log in button, it refreshes the page, so request.endpoint become "login".
I could see only 2 ways of solving this:
to force "next" argument not to change after pressing the log in button
to send from without refreshing
But both looks strange to me because i suppose there is another way to do that.
could anyone help?
I think the problem is the url inside form action, can you set action="" instead of action="{{ url_for('login', next=request.endpoint) }}"

submitting form to controller#store

laravel 5.5
voyager 1.0
i'm trying to submit a form to a controller with :
<form class="" action="FeedController#store" method="post">
<div class="mui-textfield mui-textfield--float-label">
<input id="title" type="text" name="title" value="" required autofocus>
<label for="title" class="col-md-4 control-label">title</label>
</div>
<div class="mui-textfield mui-textfield--float-label">
<textarea id="description" type="textarea" name="description" value="" required autofocus></textarea>
<label for="description" class="col-md-4 control-label">description</label>
</div>
<input class="mui-btn btn-primary pull-right " type="submit" name="" value="submit">
</form>
within FeedController:
/**
*#param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request){
$data = ReportDatum::create([
'owner_id' => auth()->id(),
'title' => request('title'),
'description' => request('description')
]);
return test;
}
and in the routes:
Route::resource('feed', 'FeedController');
(and/or depending on what I'm trying)
Route::post('store', 'FeedController#store');
what I'm i missing to correctly submit the form?
I'm not using laravelCollective forms
You're missing the csrf_field(). Try adding it after the <form> tag.
Ref: https://laravel.com/docs/5.5/csrf
You should define route at first then it will works:
Resource controller routing
Route::resource('feed', 'FeedController');
or specific simple routing
Route::post('feed', 'FeedController#store');
Same error here. I've commented the line \Greazy\Http\Middleware\VerifyCsrfToken::class which stays in app/Http/Kernel.php and the post method worked in Postman, however it does not work when invoked on the form.

Ruby On Rails "undefined method `description' for #" when save

I'm relatively new to rails and I've been struggling with this for a couple of days. I'd be much appreciated if you can see where I've gone wrong.
I got this error message
undefined method `description' for #<Invoicegeneric:0x000001054900b0>
if #invoicegeneric.save ==> error at this
response = "Success"
else
response = "Error"
and this is my controller code
def addgenericitems
#invoicegeneric = Invoicegeneric.new
#invoicegeneric.invoice_id = params[:invoice_id]
#invoicegeneric.po_id = params[:po_id]
if #invoicegeneric.save
response = "Success"
else
response = "Error"
end
render :json => {:html => render_to_string(:partial => "invoices/invoicegenerics", :layout => false) }.to_json, :status => :ok
end
and this is my html
<form id="modal-form" class="pure-form pure-form-stacked"
name="invoicegenericform" action="<%= addgenericitems_invoice_path(#invoice) %>"
method="post">
<fieldset>
<label for="po_id">PO</label>
<select name="po_id" class="chzn-select">
<% Purchaseorder.active.order(:sku).each do |p| %>
<option value="<%= p.sku %>"><%= "#{p.sku}" %></option>
<% end %>
</select>
</fieldset>
<fieldset>
<label for="sku_sj">SJ</label>
<input name="sku_sj" type="text">
</fieldset>
<div class="modal-footer">
<button class="pure-button" data-dismiss="modal" aria-hidden="true">Close</button>
<input type="submit" class="pure-button pure-button-primary" value="Tambah"/>
</div>
</form>
In my table there is no column name description

Zend_Form with CSS

i am new to zend i have coded up to some extent but dont know further how to decorate my form using decorators etc
this is my old HTML
<div class="col_6 pre_3 padding_top_60" style="padding-top:0">
<div class="widget clearfix">
<div class="widget_inside">
<p class="margin_bottom_15">Kindly enter Username and Password.</p>
<span><?= $this->error;?></span>
<form method='post' action='<?php echo $this->url(array(), 'login') ?>' onsubmit="return validateForm();">
<div class="form">
<div class="clearfix" id="div_username">
<label class="lable-2">Username</label>
<div class="input">
<input type="text" class="xlarge" name="username" id="username" value="<?php echo $this->form['username'] ?>"/>
<span id="error_message_username"></span>
</div>
</div>
<div class="clearfix" id="div_password">
<label class="lable-2">Password</label>
<div class="input">
<input type="password" class="xlarge" name="password" id="password" value="<?php echo $this->form['password'] ?>"/>
<span id="error_message_Password"></span>
</div>
</div>
<div class="clearfix">
<label></label>
<div class="input">
<input type="checkbox" name="remember" value="1" <?php echo ($this->form['remember'] == 1)?'checked="checked"':'' ?>/></span> Stay signed in
<span style="margin-left:10px">Forgot User ID or Password </span>
</div>
</div>
<div class="clearfix">
<label></label>
<div class="input">
<input type="submit" value="Login" class="medium button blue blue-1"/>
</div> </div>
</div>
</form>
</div>
</div>
This is my Form
<?php
class Application_Form_Loginform extends Zend_Form{
public function init()
{
/* Form Elements & Other Definitions Here ... */
$this->setMethod('post');
$this->addElement('text','username',array(
'label' => 'UserName :',
'required' => true,
'filters' => array('StringTrim')
));
$this->addElement('password','password',array(
'label' => 'Password',
'required' => 'true',
'filters' => array('StringTrim')
));
$this->addElement('submit','submit',array(
'ignore'=>'true',
'label'=>'Login'
));
}
}
Question is How can i make my form made with php equal to the above HTML ??
Zend Form is a pain to use, especially decorators.
You should use the View Script decorator instead.
See here: http://framework.zend.com/manual/en/zend.form.standardDecorators.html#zend.form.standardDecorators.viewScript
Aside from Zend Form decorators having a high learning curve, it mixes view code with logic and validation code.
This is not a good thing, especially if you have frontend developers styling and working with the forms.
It also makes prototyping the look of the forms difficult since you have to keep adjusting the decorator code.
With that said, Zend Form does a good job of making validation relatively painless. But one other gripe with it is that I don't think it fits well model validation.
I agree that decorators are complex, but they save a GREAT DEAL of time, when you know how to use them. We use them for our admin panel. We recently changed it from table layout to few fieldsets and divs and it took only few hours to change 70+ forms.
Your validators should be something like:
Form:
form,
htmlTag (div, class=form)
formElements
Elements:
viewHelper
error, placement => append
htmlTag (div, class input)
Label, placement => prepend
htmlTag (div, class=clearfix)
just keep in mind you need to set different keys for each htmlTag decorator, or else they would be overwritten. Like this:
....
array('inputHtmlTag' => 'htmlTag, array('tag' => 'div', 'class' => 'input'),
....
To see how I deal with complex Twitter Bootstrap decorators see https://github.com/tomasfejfar/Zend-Form-Bootstrap/blob/master/library/Bootstrap/Form.php There you can find clues how to implement your own markup.

How do I post my user name and password to a web page?

I want to post my user and password with Perl to a website. The source of the login page is:
<div class="top">Sign In</div>
<div class="row"><label for="username">Username:</label></div>
<div class="row"><div id="login:usernameContainer">
<input id="login:usernameContainer:username" type="text"
name="login:usernameContainer:username" class="username" size="16" />
</div></div>
<div class="row"><label for="password">Password:</label></div>
<div class="row"><div id="login:j_id598">
<input id="login:j_id598:password" type="password"
name="login:j_id598:password" value="" size="16" class="password" />
</div></div>
<div class="clearfix">
<div class="row rememberme">
<input id="login:rememberme" type="checkbox"
name="login:rememberme" class="rememberme" />Remember Me
</div>
<div class="submit-button">
<input type="image" src="/beta/style/shun/base/base/en/text/button/button-login.gif"
name="login:j_id603" />
</div>
</div>
What am I going to do to post my user and password?
Thanks in advance.
You use appropriate field names (you can find them in the HTML you posted) and populate the form values for whatever Perl code you use to post to the page (WWW::Mechanize or whatever else you use)
See http://search.cpan.org/dist/WWW-Mechanize/lib/WWW/Mechanize.pm for details on the module, but here's a stab at it:
use strict; use warnings;
use WWW::Mechanize;
my $mech = WWW::Mechanize->new();
$mech->get( 'http://url.to/your/form' );
$mech->submit_form(
form_number => 0, # see if it's the first form, or change this
fields => {
'login:usernameContainer:username'
=> 'username', # change to your user name
'login:j_id598:password'
=> 'password', # change to your password
}
);
If it doesn't work for you, or the fields have always different names or such, try using $mech->find_link with a regex and use the returned values to change the fields you need, then submit the form.
hope this helps