Hide Links Depending on Login

F

Frank Bishop

I'm using forms authentication with a database. I have an app that lets
users run online reports. Right now, depending on their login in the DB,
they get redirected to the pages that apply to them. I've noticed that
nothing stops them from browsing out to another users page once they log
in.

I'm thinking maybe I should just hide content instead. Is their any
simple examples of this or is my current way fixable?

Thanks,
Frank
 
Q

Q. John Chen

Here is what I did:

In db, assign roles to different users.

In Login Form, save the user role in authentication cookie, like this:
userData = "Role1";
authTicket = new FormsAuthenticationTicket(1, userName,
DateTime.Now, DateTime.Now.AddHours(12), false, userData);
authCookie = new HttpCookie(FormsAuthentication.FormsCookieName);
authCookie.Value = FormsAuthentication.Encrypt(authTicket);
Response.Cookies.Add(authCookie);

In Global.asax, add AuthenticationRequest Handler, like this:
protected void Application_AuthenticateRequest(Object sender,
EventArgs e)
{
if (Context.Request.IsAuthenticated)
{
string[] roles;
FormsIdentity identity = (FormsIdentity)
Context.User.Identity;
// IN MY CASE, a user have multiple roles and I store
roles in one column seperated by comma
roles = identity.Ticket.UserData.Split(',');
for (int i= roles.GetLowerBound(0); i <=
roles.GetUpperBound(0); i++)
{
roles = roles.Trim();
}
Context.User = new GenericPrincipal(identity, roles);
}

Now, in the page you want dynamic link.
// do something like this:
if (User.IsInRole("Sales"))
// link.Visible = false;
else
// link.Visible = true;

Hope it help

John
 
G

Guest

Instead of using cookies on the client side, you could set a server session
variable at login and on those pages that are selective you would just check
for the correct value of the session variable. If it is set right, then
allow the page. This is the way I do that on my personal web site.

Evan R. Hicks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top