0

I am fairly new to MVC development.

I have a controller called "Usercontroller" to manage SignIn, Forgot password and registration. I have added HttpGet, HttpPost methods to SignIn. HttpPost SignIn method is an overload of the SignIn function with "Model.User". I have a "Forgot Password" method as well. (Send the email).

Forgot Password is a div pop up, which has a "Send Link" functionality. If I click on "Send Link", it will also hit the "SignIn" method in the user controller. Need to identify this and call "Forgot Password" method. How can I do this? Or is there a better way to implement forgot password?

tereško
  • 58,060
  • 25
  • 98
  • 150
Dhanuka777
  • 8,331
  • 7
  • 70
  • 126

4 Answers4

1

make a private method in UserController called AuthenticateUser()

Then logIn and ForgotPassword Actions will call AuthenticateUser() before performing the rest of their logic

Scott Selby
  • 9,420
  • 12
  • 57
  • 96
  • How can I distinguish whether this call is coming from the "Login Submit" or "Forgot Password Submit"? – Dhanuka777 Dec 06 '13 at 08:33
  • @Dhanuka777 Look at my answer, for distinguishing which button has been clicked. – Max Dec 06 '13 at 15:12
  • @Dhanuka777 - That answer from Max will work , I thought you were sending LogIn button to a LogIn Action, and Forgot Password button to ForgotPasswordAction , keeping these action seperate , but sharing the same authentication function. That's the way I would do it anyway. – Scott Selby Dec 06 '13 at 23:24
1

If you keep getting redirected to the SignIn() action when hitting your '/User/ForgotPassword' url, then most likely the ForgotPassword action is requesting authentication, and as the user isn't authenticated, it redirects them to the login page.

Try adding the following into your web.config:

<location path="User/ForgotPassword">
  <system.web>
    <authorization>
      <allow users="?"/>
    </authorization>
  </system.web>
</location>

You can put this just below the end tag of the 'system.web' node, for e.g.:

<system.web>
  <authentication mode="Forms">
    <forms name="YourCookieName" loginUrl="/User/SignIn" path="/" domain="" timeout="40320" slidingExpiration="true"/>
  </authentication>
  <authorization>
    <deny users="?"/>
  </authorization>

  <!-- the rest of your system.web config in here -->
</system.web>

<location path="User/ForgotPassword">
  <system.web>
    <authorization>
      <allow users="?"/>
    </authorization>
  </system.web>
</location>

Although, this is just a guess at what your issue may be.

ctrlplusb
  • 12,847
  • 6
  • 55
  • 57
1

I think you want to know, which button has been pressed, following code gives you the solution to this problem:

Give the buttons the same name

View:

 <input type="submit" id="btn1" name="btnSubmit" value="Login" />
 <input type="submit" id="btn2" name="btnSubmit" value="Forgot Password" />

Controller:

[HttpPost]
public ActionResult ReturnFunction(Model model, string btnSubmit)
{
  switch (btnSubmit) {
    case "Login":
      //Code for checking credentials here
      break;
    case "Forgot Password":
     //Code retrieving password here
      break;
  }

The switch statement handles the button values, so you can tell which button has been clicked.

Max
  • 12,622
  • 16
  • 73
  • 101
1

We have adopted a solution for all pages that require to have multiple submit buttons per form which would be applicable in your case (have used it in login as well as other senarios). I have outlined it along with a code example here:

How to use ajax link instead of submit button for form?

In your case since you are using the popup you may have to use javascript to have the popup button call a hidden submit button included in the original form depending on how you open your dialog.

Community
  • 1
  • 1
JTMon
  • 3,189
  • 22
  • 24