ASP.NET authentication
Someone posted here that we can use ASP.NET login control to authenticate users. So I created one with custom membership provider. Login process work fine: control sends username and password, they are passed to ValidateUser of my custom membership class, that method returns "true", and "loggedin" event is fired (Login successed). BUT SiteFinity still says I am logged in as Anonymous and is not authenticated.
SecurityManager.SetAuthenticationCookie(SystemManager.CurrentHttpContext, user.ProviderName, user, true)
The parameter providerName could not be resolved when attempting to call constructor Telerik.Sitefinity.Security.UserManager(System.String providerName, System.String transactionName)
Invalid type specified Telerik.Sitefinity.Security.Data.MembershipDataProvider
I fixed the issue by moving away from ASP.NET compatible to Sitefinity membership provider.
Hi Max,
Yes, you need a Sitefinity provider to get this working. Currently we have not added pubic controls that you can use to authenticate your users directly, because of some missing implementation. You could use LoginForm control.
Sincerely yours,
Ivan Dimitrov
the Telerik team
Max,
Ivan,
Hello Andrei,
Most probably the authenticated user does not have permissions for the controls on your destination page. Check the permissions over the widgets and content data that should be displayed.
All the best,
Ivan Dimitrov
the Telerik team
Ivan,
<%@ Control Language="VB" AutoEventWireup="false" CodeFile="LogIn.ascx.vb" Inherits="LogIn" %>
<
asp:Login
ID
=
"LoginControl"
runat
=
"server"
BackColor
=
"#F7F6F3"
BorderColor
=
"#E6E2D8"
BorderPadding
=
"4"
BorderStyle
=
"Solid"
BorderWidth
=
"1px"
Font-Names
=
"Verdana"
Font-Size
=
"0.8em"
ForeColor
=
"#333333"
Height
=
"16px"
Width
=
"331px"
>
<
InstructionTextStyle
Font-Italic
=
"True"
ForeColor
=
"Black"
/>
<
LayoutTemplate
>
<
table
cellpadding
=
"4"
cellspacing
=
"0"
style
=
"border-collapse:collapse;"
>
<
tr
>
<
td
>
<
table
cellpadding
=
"0"
style
=
"height:168px;width:383px;"
>
<
tr
>
<
td
align
=
"center"
colspan
=
"2"
style
=
"color:White;background-color:#5D7B9D;font-size:medium;font-weight:bold;"
>
Log In</
td
>
</
tr
>
<
tr
>
<
td
align
=
"right"
>
<
asp:Label
ID
=
"UserNameLabel"
runat
=
"server"
AssociatedControlID
=
"UserName"
Font-Bold
=
"true"
Font-Size
=
"Medium"
>User Name: </
asp:Label
>
</
td
>
<
td
>
<
asp:TextBox
ID
=
"UserName"
runat
=
"server"
Font-Size
=
"Medium"
Width
=
"180px"
></
asp:TextBox
>
<
asp:RequiredFieldValidator
ID
=
"UserNameRequired"
runat
=
"server"
ControlToValidate
=
"UserName"
ErrorMessage
=
"User Name is required."
ToolTip
=
"User Name is required."
ValidationGroup
=
"ctl00$LoginControl"
>*</
asp:RequiredFieldValidator
>
</
td
>
</
tr
>
<
tr
>
<
td
align
=
"right"
>
<
asp:Label
ID
=
"PasswordLabel"
runat
=
"server"
AssociatedControlID
=
"Password"
Font-Bold
=
"true"
Font-Size
=
"Medium"
>Password: </
asp:Label
>
</
td
>
<
td
>
<
asp:TextBox
ID
=
"Password"
runat
=
"server"
Font-Size
=
"Medium"
TextMode
=
"Password"
Width
=
"180px"
></
asp:TextBox
>
<
asp:RequiredFieldValidator
ID
=
"PasswordRequired"
runat
=
"server"
ControlToValidate
=
"Password"
ErrorMessage
=
"Password is required."
ToolTip
=
"Password is required."
ValidationGroup
=
"ctl00$LoginControl"
>*</
asp:RequiredFieldValidator
>
</
td
>
</
tr
>
<
tr
>
<
td
align
=
"center"
colspan
=
"2"
style
=
"color:Red;"
>
<
asp:Literal
ID
=
"FailureText"
runat
=
"server"
EnableViewState
=
"False"
></
asp:Literal
>
</
td
>
</
tr
>
<
tr
>
<
td
align
=
"right"
colspan
=
"2"
>
<
asp:Button
ID
=
"LoginButton"
runat
=
"server"
BackColor
=
"#FFFBFF"
BorderColor
=
"#CCCCCC"
BorderStyle
=
"Solid"
BorderWidth
=
"2px"
CommandName
=
"Login"
Font-Names
=
"Verdana"
Font-Size
=
"Medium"
ForeColor
=
"#284775"
Text
=
"Log In"
ValidationGroup
=
"ctl00$LoginControl"
Width
=
"68px"
/>
</
td
>
</
tr
>
</
table
>
</
td
>
</
tr
>
</
table
>
</
LayoutTemplate
>
<
LoginButtonStyle
BackColor
=
"#FFFBFF"
BorderColor
=
"#CCCCCC"
BorderStyle
=
"Solid"
BorderWidth
=
"1px"
Font-Names
=
"Verdana"
Font-Size
=
"Medium"
ForeColor
=
"#284775"
/>
<
TextBoxStyle
Font-Size
=
"Medium"
/>
<
TitleTextStyle
BackColor
=
"#5D7B9D"
Font-Bold
=
"True"
Font-Size
=
"Medium"
ForeColor
=
"White"
/>
</
asp:Login
>
Imports Telerik.Sitefinity.Web.UI
Imports Telerik.Sitefinity.Security
Imports Telerik.Sitefinity.Services
Partial Class LogIn
Inherits System.Web.UI.UserControl
Private _destinationPageUrl As String = "~/Default.aspx"
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim username As String = HttpContext.Current.User.Identity.Name
SecurityManager.SetAuthenticationCookie(SystemManager.CurrentHttpContext.Response, Membership.GetUser(username).ProviderName, username, False)
LoginControl.DestinationPageUrl = _destinationPageUrl
End Sub
Public Property DestinationPageURL() As String
Get
Return _destinationPageUrl
End Get
Set(ByVal value As String)
_destinationPageUrl = value
End Set
End Property
End Class
Hello Andrei,
1. Remove the .aspx from the destination page. The url should be ~/Briefing/test
2. The second issue, it generally occurs if you use IIS7 and the managed modules are not enabled.
Best wishes,
Ivan Dimitrov
the Telerik team
Ivan,
Hi Andrei,
I prepared a short video for you.
Best wishes,
Ivan Dimitrov
the Telerik team
Ivan,
|
|
Hello Andrei,
You have added you code inside Page_Load and you are calling Membership.GetUser(username). When you are not authenticated there is no user in the context. This code should go inside LoggedIn and Authenticate events
this.Login1.LoggedIn += new EventHandler(Login1_LoggedIn);
this.Login1.Authenticate += new AuthenticateEventHandler(Login1_Authenticate);
Also, you are not making any check for null exceptions.
Kind regards,
Ivan Dimitrov
the Telerik team
Ivan,
Imports Telerik.Sitefinity.Web.UI
Imports Telerik.Sitefinity.Security
Imports Telerik.Sitefinity.Services
Partial Class LogIn
Inherits System.Web.UI.UserControl
Private _destinationPageUrl As String = "~/Default.aspx"
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
LoginControl.DestinationPageUrl = _destinationPageUrl
End Sub
Public Property DestinationPageURL() As String
Get
Return _destinationPageUrl
End Get
Set(ByVal value As String)
_destinationPageUrl = value
End Set
End Property
Protected Sub LoginControl_Authenticate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs) Handles LoginControl.Authenticate
If Membership.ValidateUser(LoginControl.UserName, LoginControl.Password) Then
e.Authenticated = True
Else
e.Authenticated = False
LoginControl.FailureText = "Authentication failed. Please try again."
End If
End Sub
Protected Sub LoginControl_LoggedIn(ByVal sender As Object, ByVal e As System.EventArgs) Handles LoginControl.LoggedIn
Try
Dim username As String = HttpContext.Current.User.Identity.Name
SecurityManager.SetAuthenticationCookie(SystemManager.CurrentHttpContext.Response, Membership.GetUser(username).ProviderName, username, False)
Catch ex As Exception
'TODO:Raise apropriate exception.
End Try
End Sub
End Class
Ivan,
Protected Sub LoginControl_LoggedIn(ByVal sender As Object, ByVal e As System.EventArgs) Handles LoginControl.LoggedIn
Try
Dim userName As String = LoginControl.UserName
If CountUsers Then
If Not Me.IsBackend OrElse Not Me.IsDesignMode Then
CountUserIn(userName)
End If
End If
'set the authentication cookie.
Dim user As Model.User = CType(Membership.GetUser(userName), Model.User)
SecurityManager.SetAuthenticationCookie(SystemManager.CurrentHttpContext.Response, user.ProviderName, user, False)
'just for test to see if cookie being set.
Dim curruser As String = SecurityManager.GetCurrentUserName
Catch ex As Exception
'TODO:Raise apropriate exception.
End Try
End Sub
Hello Andrei,
Please take a look at this post. where I sent a reply.
Best wishes,
Ivan Dimitrov
the Telerik team