I've decided to use forms authentication to log in users, unfortunetly i have some problems with that. I would like if user log in correctly to move him to some specific controller and from that point he could go over other controllers with check if he's really authorized. Of course in all controller's methods there will be check if user was really authenticated right? This is main question and 3 other questions at the bottom of this topic. Please of your support.
So far i have this code in my LoginController:
Function Index() As ActionResult
Return View()
End Function
'Action for POST method (login)
<HttpPost>
<AllowAnonymous>
Function Index(ByVal user As tbLogin) As ActionResult
Try
If (ModelState.IsValid) Then
If IsValid(user.Login, user.Password) Then
FormsAuthentication.SetAuthCookie(user.Id, False)
Return RedirectToAction("AfterLogin")
Else
ViewData("Success") = "Login error"
End If
End If
Catch ex As Exception
Return RedirectToAction("Index", "Home")
End Try
Return View(user)
End Function
'Action for Show view after login
<Authorize>
Function AfterLogin() As ActionResult
Return RedirectToAction("Index", "Home")
End If
End Function
Function IsValid(Login As String, password As String) As Boolean
Dim _isValid As Boolean = False
Using dc = New woitgroup_transport.production_WojtgroupEntitesContext
Dim user = dc.tbLogin.Where(Function(a) a.Login.Equals(Login) And a.Password.Equals(password)).FirstOrDefault()
If Not IsNothing(user) Then
If user.Password = password Then
_isValid = True
End If
End If
End Using
Return _isValid
End Function
In webconfig:
<authentication mode="Forms">
<forms loginUrl="~/Login/Index" timeout="2880" />
</authentication>
1st question: Currently in every controller and every method i marked them as <Authorized>
. I checked typing manually links to address and without user login user will be redirected to login page (because of webconfig) - seems it's working. Can you confirm this way?
e.g:
<Authorize>
Public Function Index(model As CustomModelProjetsTransports, Optional filter As String = "") As ActionResult
3rd question: Since i kept user.Id here: FormsAuthentication.SetAuthCookie(user.Id, False)
i would like to read this id in some controller. How can i read this value?
4th question: is there any timeout for since user authnticated?
5th question: Is this command correct to log out user - to break session?: FormsAuthentication.SignOut
FOR FURTHER DISCUSSIONS (with @C0dingJammer)- EXTENDED:
<HttpPost>
<AllowAnonymous>
<ValidateAntiForgeryToken>
Function Index(ByVal user As tbLogin) As ActionResult
Try
If (ModelState.IsValid) Then
Dim userId As Integer
If Not IsNothing(GetUserIdIfValid(user.Login, user.Password)) Then
userId = GetUserIdIfValid(user.Login, user.Password)
'false wywali cookie po zamknieciu browser - true zostawi
FormsAuthentication.SetAuthCookie(userId, False)
Return RedirectToAction("AfterLogin")
Else
ViewData("Success") = "Login error"
End If
End If
Catch ex As Exception
Return RedirectToAction("Index", "Home")
End Try
Return View(user)
End Function
This should only return false/true but to get also user.Id i refactored it to get this is and pass to FormsAuthentication.SetAuthCookie(userId, False). I am looking for to make it bit better than it is right now:
Function GetUserIdIfValid(Login As String, password As String) As Object
Dim _getuserId As Object = Nothing
Using dc = New woitgroup_transport.production_WojtgroupEntitesContext
Dim user = dc.tbLogin.Where(Function(a) a.Login.Equals(Login) And a.Password.Equals(password)).FirstOrDefault()
If Not IsNothing(user) Then
If user.Password = password Then
_getuserId = user.Id
End If
End If
End Using
Return _getuserId
End Function