mlawton

Hi, Does anyone know how I would go about converting the following code into C#

Partial Class Login

Inherits System.Web.UI.Page

Protected Sub Login1_LoginError(ByVal sender As Object, ByVal e As System.EventArgs) Handles Login1.LoginError

'Set the parameters for InvalidCredentialsLogDataSource

InvalidCredentialsLogDataSource.InsertParameters("ApplicationName").DefaultValue = Membership.ApplicationName

InvalidCredentialsLogDataSource.InsertParameters("UserName").DefaultValue = Login1.UserName

InvalidCredentialsLogDataSource.InsertParameters("IPAddress").DefaultValue = Request.UserHostAddress

'The password is only supplied if the user enters an invalid username or invalid password - set it to Nothing, by default

InvalidCredentialsLogDataSource.InsertParameters("Password").DefaultValue = Nothing

'There was a problem logging in the user

'See if this user exists in the database

Dim userInfo As MembershipUser = Membership.GetUser(Login1.UserName)

If userInfo Is Nothing Then

'The user entered an invalid username...

LoginErrorDetails.Text = "There is no user in the database with the username " & Login1.UserName

'The password is only supplied if the user enters an invalid username or invalid password

InvalidCredentialsLogDataSource.InsertParameters("Password").DefaultValue = Login1.Password

Else

'See if the user is locked out or not approved

If Not userInfo.IsApproved Then

LoginErrorDetails.Text = "Your account has not yet been approved by the site's administrators. Please try again later..."

ElseIf userInfo.IsLockedOut Then

LoginErrorDetails.Text = "Your account has been locked out because of a maximum number of incorrect login attempts. You will NOT be able to login until you contact a site administrator and have your account unlocked."

Else

'The password was incorrect (don't show anything, the Login control already describes the problem)

LoginErrorDetails.Text = String.Empty

'The password is only supplied if the user enters an invalid username or invalid password

InvalidCredentialsLogDataSource.InsertParameters("Password").DefaultValue = Login1.Password

End If

End If

'Add a new record to the InvalidCredentialsLog table

InvalidCredentialsLogDataSource.Insert()

End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

'If the person is already logged in, but is being redirected to the login page from some other

'page, then they were attempting to visit a page they weren't authorized for

If Request.IsAuthenticated AndAlso Request.QueryString("ReturnUrl") IsNot Nothing Then

LoginErrorDetails.Text = "You are logged in, but have attempted to visit a page for which you are not authorized."

End If

End Sub

End Class

Cheers, Mark



Re: Visual C# Language Translating from vb

David Anton

(via Instant C# - let me know how the converted code works for you)

internal partial class Login : System.Web.UI.Page
{
override protected void OnInit(EventArgs e)
{
base.OnInit(e);

//INSTANT C# NOTE: Converted event handlers:
Login1.LoginError += new System.EventHandler(Login1_LoginError);
}

protected void Login1_LoginError(object sender, System.EventArgs e)
{
//Set the parameters for InvalidCredentialsLogDataSource
InvalidCredentialsLogDataSource.InsertParameters["ApplicationName"].DefaultValue = Membership.ApplicationName;
InvalidCredentialsLogDataSource.InsertParameters["UserName"].DefaultValue = Login1.UserName;
InvalidCredentialsLogDataSource.InsertParameters["IPAddress"].DefaultValue = Request.UserHostAddress;

//The password is only supplied if the user enters an invalid username or invalid password - set it to Nothing, by default
InvalidCredentialsLogDataSource.InsertParameters["Password"].DefaultValue = null;

//There was a problem logging in the user
//See if this user exists in the database
MembershipUser userInfo = Membership.GetUser(Login1.UserName);
if (userInfo == null)
{
//The user entered an invalid username...
LoginErrorDetails.Text = "There is no user in the database with the username " + Login1.UserName;

//The password is only supplied if the user enters an invalid username or invalid password
InvalidCredentialsLogDataSource.InsertParameters["Password"].DefaultValue = Login1.Password;
}
else
{
//See if the user is locked out or not approved
if (! userInfo.IsApproved)
{
LoginErrorDetails.Text = "Your account has not yet been approved by the site's administrators. Please try again later...";
}
else if (userInfo.IsLockedOut)
{
LoginErrorDetails.Text = "Your account has been locked out because of a maximum number of incorrect login attempts. You will NOT be able to login until you contact a site administrator and have your account unlocked.";
}
else
{
//The password was incorrect (don't show anything, the Login control already describes the problem)
LoginErrorDetails.Text = string.Empty;

//The password is only supplied if the user enters an invalid username or invalid password
InvalidCredentialsLogDataSource.InsertParameters["Password"].DefaultValue = Login1.Password;
}
}

//Add a new record to the InvalidCredentialsLog table
InvalidCredentialsLogDataSource.Insert();
}

//ORIGINAL LINE: Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles this.Load
override protected void OnLoad(System.EventArgs e)
{
base.OnLoad(e);
//If the person is already logged in, but is being redirected to the login page from some other
//page, then they were attempting to visit a page they weren't authorized for
if (Request.IsAuthenticated && Request.QueryString["ReturnUrl"] != null)
{
LoginErrorDetails.Text = "You are logged in, but have attempted to visit a page for which you are not authorized.";
}
}
}

David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C# to C++ converter, VB to C++ converter
Instant Python: C# to Python converter, VB to Python converter






Re: Visual C# Language Translating from vb

mlawton

Thanks very much for that, I'll try it when back at work on Monday and let you know how I get on.

Mark