Click here to Skip to main content
15,892,575 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello,

Does anyone know of a tutorial or would like to post how to do a login with roles. When the user login I would like to redirect them to a particular page. Via: Admin page or Partners page, Customer Page. Also must not be able to enter either of those pages without signing in. I found plenty of code that I can get to a Admin page without having to sign in. I also tried the ASP.NET Configuration but it seemed like over kill. Plus I can't seem to get it to do what I'm looking for.

Any help would be greatly appreciated. Thanks

What I have tried:

I found plenty of code that I can get to a Admin page without having to sign in. I also tried the ASP.NET Configuration but it seemed like over kill. Plus I can't seem to get it to do what I'm looking for.
Posted
Updated 23-Jun-16 1:02am
Comments
F-ES Sitecore 3-Jun-16 5:56am    
Google "asp.net identity" or "asp.net membership" (although membership has been superseded by Identity). That will give you everything you need. It's a fairly large, involved topic. When you create a new site in VS there is an option to have Identity added in the base project (select Individual user accounts as an auth option).

Are you using MVC? If so, what version?

If not, you must use Session and below is the logic on how to make it work.

Set the user type in DB, then when the user logs in, get the user type and create the Session based on user type.

Like this:
if (userType == 1) { Session["Admin"] = objUserAdmin; } and so on

Then on Page_Load from your Index.aspx on Admin area, just check if the Session["Admin"] != null... if it's null, just redirect the user, because he isn't admin.

You must adapt your code to work with all of your user type

Hope it helps!
 
Share this answer
 
Comments
Member 9320578 2-Jun-16 20:59pm    
I'm not using MVC. ASP.NET 4.0 I appreciate your reply but the thing is I don't know whether I should use the ASP.NET Configuration or not. I don't want to but then I'm stuck on the security part. Do you have any suggestions on how to start this project?
Thiago Vaini 2-Jun-16 21:11pm    
Well, I've never used ASP.NET Configuration files to do this, but when you use Sessions, there's no how to a common user, get access to admin page, once you're setting the Session based on the user type
Lets take it as you have a role on your database as A(admin) U(user)
Use below code to check the role and redirect to their desired page

C#
string Utype;
           Utype = dt.Rows[0][10].ToString().Trim();// This will count and select the role A or U from your table.so remember to change the rows number according to your table data
           if (Utype == "U")
           {
               Session["Username"] = UserName.Text;
               Response.Redirect("UserHome.aspx");
           }
           else if (Utype == "A")
               Session["Username"] = UserName.Text;
           Response.Redirect("~/Admin/Admin_Home.aspx");

       }


       else
       {
           lblError.ForeColor = Color.Red;
           lblError.Text = "Invalid Username or Password";
       }
       }
 
Share this answer
 
There are several good books using ASP.NET on the subject. Get one of those. It doesn't need to be the latest version, so you can get a used book for very little.
 
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