Hi have a form that call an Action properly. But when i enable the authentication forms, the action to login doesn't fire up (i test it with a breakpoint).
My Web.config is:
<authentication mode="Forms">
<forms loginUrl="Autenticacao/Index"></forms>
</authentication>
<authorization>
<deny users="?"/>
</authorization>
It's in the view Index of controller Autenticacao where i have my form:
<% Html.BeginForm("Login", "Autenticacao"); %>
<input type="text" id="autenticacaoReferencia" name="autenticacaoReferencia" /><br />
<input type="text" id="autenticacaoPassword" name="autenticacaoPassword" /><br />
<input type="submit" value="Entrar" />
<% Html.EndForm(); %>
The controller Login is sucefully called just if the authentication form is disable.
Am i using the BeginForm in a wrong way?
Edit: The Login action:
public ActionResult Login(string autenticacaoReferencia, string autenticacaoPassword, string returnUrl)
{
...
}
Thanks
Are you decorating your actions with the [Authorize] attribute?
Related
I have a login and a register form on one and the same page and I try to return the global error message only on the concerning form. How to I do that correctly?
import org.omnifaces.util.Messages;
// login class submit()
Messages.addGlobalError('Login failed');
// register class submit()
Messages.addGlobalError('Register failed');
<h:form id="login-form">
<h:messages globalOnly="true" />
// [...]
<h:commandButton value="Login" action="#{login.submit()}" />
</h:form>
<h:form id="register-form">
<h:messages globalOnly="true" />
// [...]
<h:commandButton value="Sign up" action="#{register.submit()}" />
</h:form>
That is a contradictory... you simply never assign a global error to a specific component (the form is a component).
Simply make an error that you assign to the id of the form by using
Messages.addError(id, error);
add a <h:message for="..."> where the for contains the id of the form.
Here is how your example should look like:
import org.omnifaces.util.Messages;
// login class submit()
Messages.addError('login-form', 'Login failed');
// register class submit()
Messages.addError('register-form', 'Register failed');
<h:form id="login-form">
<h:messages globalOnly="true" for="login-from" />
// [...]
<h:commandButton value="Login" action="#{login.submit()}"/>
</h:form>
<h:form id="register-form">
<h:messages globalOnly="true" for="register-from" />
// [...]
<h:commandButton value="Sign up" action="#{register.submit()}"/>
</h:form>
If I have a form that needs to use a textbox input like as below:
#{
if(IsPost){
username = Request.Form["username"]
}
}
<form action="Home/Index" method="post">
<input type="text" name="username" />
<input type="submit" value="submit" />
</form>
The controller is something like below,
public class HomeController : Controller
{
public ActionResult Index (string username) {
if (string username != string.Empty)
{
Console.WriteLine("Your username is " + username);
}
return View();
}
}
Seems like the data is not being passed from the post method. When I hit submit the URL that it requests is Home/Home/Index, which is not were the controller(HomeController) action is located, it should be Home/Index, and use the HomeController right?
What if I need to pass this data to a different controller that has an Index for the action, like UserController?
In this instance, you're missing a slash!
<form action="/Home/Index" method="post">
<input type="text" name="username" />
<input type="submit" value="submit" />
</form>
But when using asp.net mvc, you should use Url.Content with the home directory character to ensure that if your site is deployed in a sub-directory of the site, the correct root will be found. So use:
<form action="#Url.Content("~/Home/Index")" method="post">
<input type="text" name="username" />
<input type="submit" value="submit" />
</form>
I have an application with Login and Logout actions. After pressing Logout button, I would like to redirect to Login page and display here faces message with an information about a successful logging out. Now, I'm struggling with forwarding of faces message. This message doesn't want to display in Login dialog. Could you please give me any hint or recommend me another way? I'm using JSF 2.1 with Spring Security 4. What I have tried:
Logout method
public String doLogout() throws ServletException, IOException {
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
HttpServletRequest request = ((HttpServletRequest) externalContext.getRequest());
RequestDispatcher dispatcher = request.getRequestDispatcher("/j_spring_security_logout");
dispatcher.forward((ServletRequest) externalContext.getRequest(), (ServletResponse) externalContext.getResponse());
facesContext.addMessage("", new FacesMessage("You have been successfully logged out."));
externalContext.getFlash().setKeepMessages(true);
FacesContext.getCurrentInstance().responseComplete();
return null;
}
Login XHTML
<h:form id="loginForm" prependId="false">
<p:dialog visible="true" resizable="false" closable="false" draggable="false" position="center">
<p:messages globalOnly="true" id="msgs"/>
<h:outputLabel for="j_username" value="User" />
<p:inputText id="j_username" required="true"/>
<h:outputLabel for="j_password" value="Password" />
<p:password id="j_password" required="true"/>
<p:commandButton icon="ui-icon-check"
id="login"
action="#{loginController.doLogin}"
oncomplete="handleLoginRequest(xhr, status, args)"
update="msgs"
ajax="false"/>
</p:dialog>
</h:form>
Spring security
<http auto-config="true" >
<intercept-url pattern="/pages/**"
access="ROLE_RA"/>
<form-login login-page="/pages/userLogin.xhtml"
default-target-url="/pages/specimens/specimenOverview.xhtml" />
<logout logout-success-url="/pages/userLogin.xhtml"/>
<session-management invalid-session-url="/pages/userLogin.xhtml">
<concurrency-control max-sessions="1"/>
</session-management>
</http>
Just and parameter to logout url:
<logout logout-success-url="/pages/userLogin.xhtml?logout=true"/>
check it on login page and display your message:
<h:outputText value="Your logout message here" rendered="#{param['logout']}" />
My recaptcha post action method is not hitting when i submit the page.
It showing in fiddler that it is going to post method. but not hitting in my VS2010. previously it was worked when i use the mysql data base not used entity framework. now we are using sql2012 and entity framework. is any references are missing. or input parameters are not accessing properly in post method. i am not getting any point. can any one help. How can i fix this.
view:
<div class="left-div-user">
<div class="t-32_b_user">
Forgot password?</div>
<div class="t-12-user">
Email
</div>
<div>
#Html.TextBoxFor(model => model.Email, new { #class = "user-input-bx" })</div>
<div style="font: 20px; color: Black; font-weight: bold;">
Please enter the below words in the box.
</div>
<div class="t-12_2-user">
#Html.ValidationMessageFor(model => model.Email)</div>
<div>
#Html.Raw(Html.GenerateCaptcha("captcha", "clean"))
#Html.ValidationMessage("captcha")
</div>
<input name="" type="submit" class="user-common-btn1" value="Submit" />
</div>
ActionMethod:
[HttpPost]
[AllowAnonymous]
[RecaptchaControlMvc.CaptchaValidator]
public ActionResult ForgotPassword(ForgotPasswordModel fp, bool CaptchaValid, String CaptchaErrorMessage)
{
//my code
}
web.config:
<appSettings>
<add key="ReCaptchaPrivateKey" value="privatevalue" />
<add key="ReCaptchaPublicKey" value="publicvalue" />
</appSettings>
<system.web>
<namespaces>
<add namespace="Recaptcha" />
</namespaces>
</system.web>
I've created a custom helper that renders a grid and receives the strongly typed view's model as a parameter.
Basically my view looks like this:
<% using (Html.BeginForm("UpdateValues", "Home", FormMethod.Post)) { %>
<%= Html.MyGrid(Model)%>
<input type="submit" value="Update Values" />
<%} %>
But when I click on the submit button, all the values on the model are null.
This is what the controller looks like:
[HttpPost]
public string UpdateValues(AssignmentResultsVm assignmentResults)
{
//..... do something
}
How can I make this work?
Thanks in advance.
You need to make sure that the items are arranged in your grid so that the default model binder can map the data map to your view model class.
This is done by index the name property of the data your are binding like this:
<form method="post" action="/Home/Create">
<input type="text" name="[0].Title" value="Curious George" />
<input type="text" name="[0].Author" value="H.A. Rey" />
<input type="text" name="[1].Title" value="Code Complete" />
<input type="text" name="[1].Author" value="Steve McConnell" />
<input type="submit" />
</form>
You can have this done for you by the EditorTemplates feature of asp.net mvc which is exemplified in this article.