I am new to this whole MVC stuff, so please bear with me.
I am wondering what is the correct way to implement controller logic.
In other words, take this very simple example, a forgot password screen. In traditional ASP/ASP.Net, this could eaisly be handled in one page, hide/show a div or two based on the flow... done!!
I have come up with the following and just wanted to see if I was on the right track. Have a look at the following controller:
Namespace Controllers
Public Class AuthenticationController
Inherits ControllerBase
Private MembershipProvider As New GTGMembershipProvider
<HttpGet()>
Function LogOn() As ActionResult
If (User.Identity.IsAuthenticated) Then
Return RedirectToAction("Index", "Main")
End If
Return View(New LogOnViewModel)
End Function
<HttpPost()>
Function LogOn(Model As LogOnViewModel, ReturnUrl As String) As ActionResult
If (Not ModelState.IsValid) Then
Return View(Model)
End If
If (Not MembershipProvider.ValidateUser(Model.UserName, Model.Password)) Then
ModelState.AddModelError("", "Invalid login. Incorrect password/user name.")
Return View(Model)
End If
IssueAuthenticationTicket(Model)
If (Not ReturnUrl.IsNullOrEmpty) Then
Return Redirect(ReturnUrl)
Else
Return RedirectToAction("Index", "Main")
End If
End Function
Function LogOff() As ActionResult
FormsAuthentication.SignOut()
Return RedirectToAction("Index", "Main")
End Function
<HttpGet()>
Function ForgotPassword() As ActionResult
Return View(New ForgotPasswordViewModel)
End Function
<HttpPost()>
Function ForgotPassword(Model As ForgotPasswordViewModel) As ActionResult
If (Not ModelState.IsValid) Then
Return View(Model)
End If
Return RedirectToAction("PasswordSent")
End Function
<HttpGet()>
Function PasswordSent() As ActionResult
Return View()
End Function
Private Sub IssueAuthenticationTicket(Model As LogOnViewModel)
Dim Profile As New CustomerProfile With {.FirstName = "Sam", .ID = 1, .LastName = "Striano"}
Dim Ticket As New FormsAuthenticationTicket(1, Model.UserName, Now, Now.AddDays(30), Model.RememberLogon, Profile.ToString)
Dim Cookie As New HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(Ticket))
HttpContext.Response.Cookies.Add(Cookie)
End Sub
End Class
End Namespace