asp.net can only login in firefox - asp.net

in my asp.net website i can only login in Firefox but not in chrome, internet explorer or safari.
here is my code :
string userName = LoginUserName.Text;
string password = LoginPassword.Text;
if (Page.IsValid)
{
if (Membership.ValidateUser(userName, password))
{
if (RemeberMe.Checked == true)
{
Response.Redirect("~/Home.aspx");
}
}
}
here is the login table :
<table>
<tr>
<td>
<asp:Label Text="Email:" AssociatedControlID="LoginUserName" runat="server"
id="LoginUserNamelabel" CssClass="label"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:TextBox ID="LoginUserName" runat="server" Width="250px" CssClass="textbox"/>
<asp:RequiredFieldValidator id="LoginEmailRequired" runat="server" ControlToValidate="LoginUserName"
ErrorMessage="Email is required" ToolTip="Email is required"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:Label Text="Password:" AssociatedControlID="LoginPassword" runat="server"
ID="LoginPasswordlabel" CssClass="label"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:TextBox ID="LoginPassword" runat="server" TextMode="Password" Width="250px" CssClass="textbox" />
<asp:RequiredFieldValidator ID="PassRequired" runat="server" ControlToValidate="LoginPassword"
ErrorMessage="Password is Required" ToolTip="Password is Required"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
<asp:CheckBox ID="RemeberMe" runat="server" Text="Remember me" CssClass="RemeberMe" />
</td>
</tr>
<tr>
<td><br />
<asp:Button ID="LoginButton" runat="server" Text="Login" OnClick="autoLogin" CssClass="Button" />
</td>
</tr>
<tr>
<td><br />
<asp:Label ID="FailureText" runat="server" EnableViewState="false" Visible="false" CssClass="Lfaluire">
Wrong Email or Password</asp:Label>
</td>
</tr>
</table>
using the AspNetSqlMembershipProvider.
i am using asp.net 4.0 , what is the problem
thanks

You always validate the username and password, but then only actually redirect if the user has checked the "Remember me" - This looks wrong. Could there be a co-incidence that you ticked the box in some test sessions but not others? ;)
Also you're not actually storing the fact that the user has logged in anywhere, so won't you have problems later with the User.Identity.IsAuthenticated still being false?
You should be using something like
FormsAuthentication.RedirectFromLoginPage Method whether or not the checkbox is checked...
If you want to do the redirect yourself you should be using FormsAuthentication.SetAuthCookie and then doing the redirect.
EDIT: Something like this
string userName = LoginUserName.Text;
string password = LoginPassword.Text;
if (Page.IsValid)
{
if (Membership.ValidateUser(userName, password))
{
if (RemeberMe.Checked)
{
// Set your own cookie here or something that you will later check for in Page_Load etc
}
// Need to tell ASP.NET authentication was successful
System.Web.Security.FormsAuthentication.SetAuthCookie(userName, True)
Response.Redirect("~/Home.aspx");
}
}

If you are hitting http://localhost then most of the browsers won't save the cookie that's being generated by your membership provider.
If this is the case, access it by a name that has a dot in it. See:
Cookies on localhost with explicit domain

Are cookies enabled in all these browsers? The most common culprit to the out-of-the-box ASP.NET login stuff "not working" is because by default, they have no fallback for when cookies are disabled.

Related

How to make asp .net to forget entered text in fields?

Everywhere I read questions about how to remember it, but I want to reset it!
I use this code:
<table>
<tr>
<td>Your name:</td>
<td><asp:TextBox runat="server" ID="user_name" MaxLength="20" AutoCompleteType="None" /></td>
</tr>
<tr>
<td><table><tr><td>Captcha:</td><td><img src="turing.aspx" alt="Enable images to see captcha image" /></td></tr></table></td>
<td><asp:TextBox runat="server" ID="txt_captcha" MaxLength="5" AutoCompleteType="None" /></td>
</tr>
<tr>
<td>Email for new messages reports:</td>
<td><asp:TextBox runat="server" ID="txt_eml" MaxLength="100" AutoCompleteType="None" /> (*this is not required)</td>
</tr>
<tr>
<td>Message text:</td>
<td><asp:TextBox runat="server" ID="comment_text" Columns = "30" Rows = "5" Wrap = "true" TextMode="MultiLine" AutoCompleteType="None"/></td>
</tr>
<tr>
<td colspan = "2">
<asp:Button runat="server" ID="Button1" Text="Add message" OnClick="HandleAddMessage" CausesValidation="true" ValidationGroup="second_group" />
</td>
</tr>
</table>
At HandleAddMessage I even reset those manually that way: user_name.Text = txt_eml.Text = txt_captcha.Text = comment_text.Text = string.Empty;
But after message added, if I refresh the page old values comes back.
What I can do to prevent it?
Once the user has submitted the form the first time, just response.redirect to the same page again. It effectively resets the page, so if they do refresh it, they won't post back the same data. It would also mean that your fields will all be reset since the page has effectively reloaded.

CreateNewUser and Send Verification Email

I have a registration page with the following code: -
<%# Page Language="C#" %>
<%# Import Namespace="System.Web.Security" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
public void CreateUser_OnClick(object sender, EventArgs args)
{
// Create new user and retrieve create status result.
MembershipCreateStatus status;
string passwordQuestion = "";
string passwordAnswer = "";
if (Membership.RequiresQuestionAndAnswer)
{
passwordQuestion = PasswordQuestionTextbox.Text;
passwordAnswer = PasswordAnswerTextbox.Text;
}
try
{
MembershipUser newUser = Membership.CreateUser(UsernameTextbox.Text, PasswordTextbox.Text,
EmailTextbox.Text, passwordQuestion,
passwordAnswer, true, out status);
if (newUser == null)
{
Msg.Text = GetErrorMessage(status);
}
else
{
Response.Redirect("~/ok_reg.aspx");
}
}
catch
{
Msg.Text = "An exception occurred creating the user.";
}
}
public string GetErrorMessage(MembershipCreateStatus status)
{
switch (status)
{
case MembershipCreateStatus.DuplicateUserName:
return "Username already exists. Please enter a different user name.";
case MembershipCreateStatus.DuplicateEmail:
return "A username for that e-mail address already exists. Please enter a different e-mail address.";
case MembershipCreateStatus.InvalidPassword:
return "The password provided is invalid. Please enter a valid password value.";
case MembershipCreateStatus.InvalidEmail:
return "The e-mail address provided is invalid. Please check the value and try again.";
case MembershipCreateStatus.InvalidAnswer:
return "The password retrieval answer provided is invalid. Please check the value and try again.";
case MembershipCreateStatus.InvalidQuestion:
return "The password retrieval question provided is invalid. Please check the value and try again.";
case MembershipCreateStatus.InvalidUserName:
return "The user name provided is invalid. Please check the value and try again.";
case MembershipCreateStatus.ProviderError:
return "The authentication provider returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
case MembershipCreateStatus.UserRejected:
return "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
default:
return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Create User</title>
</head>
<body>
<form id="form1" runat="server">
<h3>Create New User</h3>
<br />
<table cellpadding="3" border="0">
<tr>
<td>Account ID:</td>
<td><asp:Textbox id="UsernameTextbox" runat="server" /></td>
<td><asp:RequiredFieldValidator id="UsernameRequiredValidator" runat="server"
ControlToValidate="UserNameTextbox" ForeColor="red"
Display="Static" ErrorMessage="Required" /></td>
</tr>
<tr>
<td>Password:</td>
<td><asp:Textbox id="PasswordTextbox" runat="server" TextMode="Password" /></td>
<td><asp:RequiredFieldValidator id="PasswordRequiredValidator" runat="server"
ControlToValidate="PasswordTextbox" ForeColor="red"
Display="Static" ErrorMessage="Required" /></td>
</tr>
<tr>
<td>Confirm Password:</td>
<td><asp:Textbox id="PasswordConfirmTextbox" runat="server" TextMode="Password" /></td>
<td><asp:RequiredFieldValidator id="PasswordConfirmRequiredValidator" runat="server"
ControlToValidate="PasswordConfirmTextbox" ForeColor="red"
Display="Static" ErrorMessage="Required" />
<asp:CompareValidator id="PasswordConfirmCompareValidator" runat="server"
ControlToValidate="PasswordConfirmTextbox" ForeColor="red"
Display="Static" ControlToCompare="PasswordTextBox"
ErrorMessage="Confirm password must match password." />
</td>
</tr>
<tr>
<td>Email Address:</td>
<td><asp:Textbox id="EmailTextbox" runat="server" /></td>
<td><asp:RequiredFieldValidator id="EmailRequiredValidator" runat="server"
ControlToValidate="EmailTextbox" ForeColor="red"
Display="Static" ErrorMessage="Required" /></td>
</tr>
<% if (Membership.RequiresQuestionAndAnswer) { %>
<tr>
<td>Password Question:</td>
<td><asp:Textbox id="PasswordQuestionTextbox" runat="server" /></td>
<td><asp:RequiredFieldValidator id="PasswordQuestionRequiredValidator" runat="server"
ControlToValidate="PasswordQuestionTextbox" ForeColor="red"
Display="Static" ErrorMessage="Required" /></td>
</tr>
<tr>
<td>Password Answer:</td>
<td><asp:Textbox id="PasswordAnswerTextbox" runat="server" /></td>
<td><asp:RequiredFieldValidator id="PasswordAnswerRequiredValidator" runat="server"
ControlToValidate="PasswordAnswerTextbox" ForeColor="red"
Display="Static" ErrorMessage="Required" /></td>
</tr>
<% } %>
<tr>
<td colspan="3">
<asp:Label id="Msg" ForeColor="maroon" runat="server" /></td>
</tr>
<tr>
<td><asp:Button id="CreateUserButton" Text="Create User" OnClick="CreateUser_OnClick" runat="server" /></td>
<td><asp:Button id="PasswordRecover" Text="Password Recover" runat="server" PostBackUrl="~/recover.aspx" /></td>
</tr>
</table>
</form>
</body>
</html>
Here what i have to do is create a new user and send a verification mail to the email id provided by the user. User is being created without any problem, But I am finding bit hard to insert code for sending email id.
A little help is much appreciated. I need some guidance to send verification email to new users.
The System.Net.Mail namespace contains the various classes that are related to email. You can use some of the classes to send email. More information on this here
Also, it is recommended that the server-side processing code be put in the aspx.cs file (code behind) than embedding directly into the ASPX page.

displaying custom message on custom errors in validation summary control on client side?

I am using validation summary control to display error messages of asp.net validation controls.
There are some validations in page for which validation controls are not being used. I am using custom javascript and jquery code for these. Kindly guide how I can display messages of these errors in validation summary control along with asp.net validation controls.
The key is to use the Custom Validation control. This control supports client side scripting (so you can still use your javascript code to do it) but still ties into the validation framework asp.net provides.
From the microsoft article, something like this:
<SCRIPT LANGUAGE="JavaScript">
function validateLength(oSrc, args){
args.IsValid = (args.Value.length >= 8);
}
</SCRIPT>
<asp:Textbox id="text1" runat="server" text="">
</asp:Textbox>
<asp:CustomValidator id="CustomValidator1" runat=server
ControlToValidate = "text1"
ErrorMessage = "You must enter at least 8 characters!"
ClientValidationFunction="validateLength" >
</asp:CustomValidator>
As Patrick said you can add the custom validator controls in place of your custom javascript/jQuery code. And then accordingly you can add the summary validation control. Also you can change the error message in your javascript code. Please check the below code for your reference.
<script type="text/javascript">
function validatetxtLength(source, args)
{
var txtVal=document.getElementById('<%=txtusername.ClientID %>').value;
if(txtVal=="")
{
document.getElementById('<%=custxtValidator.ClientID %>').setAttribute("errormessage","Please Enter the User Name");
args.IsValid=false;
}
else if(txtVal.length>9)
{
document.getElementById('<%=custxtValidator.ClientID %>').setAttribute("errormessage","Username must have less than 10 characters");
args.IsValid=false;
}
else
{
args.IsValid=true;
}
return;
}
</script>
<div>
<table cellpadding="0" cellspacing="0" border="0" width="712px">
<tr>
<td colspan="3" align="center">
Validator Testing
</td>
</tr>
<tr>
<td>
Please Enter your User Name:
</td>
<td>
<asp:TextBox ID="txtusername" runat="server" Width="150px"></asp:TextBox>
</td>
<td>
<asp:CustomValidator ID="custxtValidator" runat="server" ErrorMessage="User Name must have less than 10 characters"
Text="*" ForeColor="Red" ClientValidationFunction="validatetxtLength"></asp:CustomValidator>
</td>
</tr>
<tr>
<td colspan="3" align="center">
<asp:Button ID="btnsubmit" runat="server" Text="Submit" />
</td>
</tr>
</table>
<asp:ValidationSummary ID="ValidationSummary1" HeaderText="Please check below validations:"
runat="server" DisplayMode="BulletList" EnableClientScript="true" />
</div>
This might be useful for you.

How can I determine whether any checkbox is checked?

I have some checkboxes in vb.net code.
<tr>
<td colspan="2">
<asp:CheckBox ID="chkbxCreateAmendOrg" runat="server" Checked="False" Text="Create/Amend Organisation" />
</td>
<td colspan="2">
<asp:CheckBox ID="chkbxCreateAmendCPUser" runat="server" Checked="False" Text="Create/Amend CP User" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:CheckBox ID="chkbxDeleteOrg" runat="server" Checked="False" Text="Delete Organisation" />
</td>
<td colspan="2">
<asp:CheckBox ID="chkbxDeleteCPUser" runat="server" Checked="False" Text="Delete CP User" />
</td>
</tr>
I want to give alert to user if they have not selected atleast one. Can i have jquery code for this
You can select all the not checked checkboxes and check the length or size() of the jQuery object:
if ($('input:checkbox:not(:checked)').length > 0) {
// some checkboxes not checked
}
Something like this should get it done...
$(document).ready(function() {
// get all checked
var checkboxes = $("input:checkbox:checked");
if(checkboxes.size() == 0)
alert("Please mark a checkbox!");
});
The following code print an alert for each checkbox not flagged:
$("input:not(:checked)").each(function(){
alert( $(this).attr("id") + " isn't checked!" );
});
See also the selectors
I advice use jQuery Validation plugin. It is very simple to use and so elegant (nice way to display error messages).
Check that example. (There are two radio buttons which one should be checked.)

Recaptcha ErrorMessage Property in ASP.NET

I am using recaptcha in an ASP.NET 3.5 application and for some odd reason the ErrorMessage property is not working. Below is my code
<recaptcha:RecaptchaControl ID="recaptcha" runat="server" PublicKey="*******************************" PrivateKey="*******************" Theme="white" ErrorMessage="This is an typo error" />
When the typed text doesn't match with the recaptcha image text it still shows the default error message "Incorrect. Try again." instead of my custom error message. What could be the reason for this strange behavior
You put the message in a Literal tag. Here is my code snippet:
<tr>
<td align="center" colspan="2" >
<recaptcha:RecaptchaControl Theme="white" ID="recaptcha" runat="server" PrivateKey="*************"
PublicKey="*************" />
</td>
</tr>
<tr>
<td align="center" colspan="2" style="color: red">
<asp:Literal ID="FailureText" runat="server" EnableViewState="False" Text="You have not entered the verification words correctly. Please try again."></asp:Literal>
</td>
</tr>
Mike

Resources