Click here to Skip to main content
15,867,834 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new in ASP.NET and I am facing problem using Login Control that is built-in with ASP.NET.

I want to build an web application that has login control. I used ASP.NET Configuration wizard to setup the login authentication config. I have created 2 users and 2 roles allready, but the problem is, I can not login using the username/password of the user allready created in the database.

I have following code written in Login_Authenticate event:

C#
if (User.Identity.IsAuthenticated == false)
                Response.Redirect("frmUserLogin.aspx");
            else if (Roles.IsUserInRole("Member"))
            {
                Response.Redirect("~/Protected/frmMemberHome.aspx");
            }

First if condition refers to the situation when authentication fails else I have checked for the roles in which my user is included. Based on their roles, I have redirected them to different pages. That is all my plan to evaluate.

Problem is each time I am trying to login using correct username/password for existing user, it fails and shows error messages for Authentication Failure. I don't have any idea. I dont know, If I am doing wrong or not! Please help me out!
Posted
Comments
Sandeep Mewara 19-Apr-12 12:58pm    
Where/which part of the code you are authenticating here?

The above code lines just check if authenticated or not - its a step after verification of authentication using username/password.
Ed Nutting 19-Apr-12 13:15pm    
You have a response below from OP.
Just thought I'd let you know,
Ed
Sandeep Mewara 19-Apr-12 14:26pm    
Thanks Ed!
Jyoti Choudhury 19-Apr-12 13:02pm    
But, each time login fails and a failure message is displayed. But as long as username/password is correct then it should validate and set the isAuthenticated to True...If I am wrong, can you please give me solution how to solve!

Look here:
Traditional Login page design discussion[^]

Login Controls were released by Microsoft, have a look at them here[^].
For, building Login Page using Login control, look:
How to: Create an ASP.NET Login Page[^]
ASP.NET Login Controls Overview[^]
How to add a Login, Roles and Profile system to an ASP.NET 2.0 [^]

If needed, pick any example from here: Google Search result on the same[^].
 
Share this answer
 
Comments
Espen Harlinn 19-Apr-12 15:56pm    
Good links, 5'ed!
Sandeep Mewara 19-Apr-12 16:13pm    
Thanks Espen.
Jyoti Choudhury 20-Apr-12 23:42pm    
thanks...
sravani.v 21-Apr-12 0:20am    
My 5!
I have tried several times and I came to the point that this problem occurs only when I put the code in Login.Authenticate event. From MSDN, it's almost clear that Authenticate event raises when user use Login control to log into the system. The main objective of this event is to perform any tasks while checking the user for authentication.

So,I tried Login.LoggedIn event that raises just after the Login.Authenticate event. LoggedIn event is responsible for any tasks that is needed to perform after validation (e.g. Accessing to the per-user data, redirecting user to different pages etc.)

For my case, I need LoggedIn event, So I tried the following code:

frmUserLogin.aspx.cs File:

//code behind file

C#
protected void Login_LoggedIn(object sender, EventArgs e)
{
        if (Roles.IsUserInRole(Login.UserName, "Member"))
             Response.Redirect("~/Protected/Member/frmMemberHome.aspx");
        else if (Roles.IsUserInRole(Login.UserName, "Admin"))
             Response.Redirect("~/Protected/Admin/frmAdminPanel.aspx");
}


HTML Code for frmUserLogin.aspx

ASP.NET
<![CDATA[<%@ Page Title="" Language="C#" MasterPageFile="~/mainLayout.Master" AutoEventWireup="true" CodeBehind="frmUserLogin.aspx.cs" Inherits="OnlineTMS_MIST.frmUserLogin" %>
<asp:content id="Content1" contentplaceholderid="head" runat="server" xmlns:asp="#unknown">
</asp:content>
<asp:content id="Content2" contentplaceholderid="ContentPlaceHolder1" runat="server" xmlns:asp="#unknown">
    <asp:login id="Login" runat="server">
        DisplayRememberMe="False" OnLoggedIn="Login_LoggedIn"
</asp:login>
    </asp:content>


It works for me absolutely fine. Thanks a lot.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900