Wrox Programmer Forums
|
BOOK: Professional ASP.NET 2.0 and Special Edition; ISBN: 978-0-7645-7610-2; ISBN: 978-0-470-04178-9
This is the forum to discuss the Wrox book Professional ASP.NET 2.0 Special Edition by Bill Evjen, Scott Hanselman, Devin Rader, Farhan Muhammad, Srinivasa Sivakumar; ISBN: 9780470041789
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional ASP.NET 2.0 and Special Edition; ISBN: 978-0-7645-7610-2; ISBN: 978-0-470-04178-9 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 20th, 2007, 02:59 AM
Registered User
 
Join Date: Mar 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to bignermo
Default FormAuthentication IsInRole() problem

Hi everyone.

I am having a little problem with forms authentication...

In Global.asax at Application_AuthenticateRequest event, I am firing the following code:

Code:
if (HttpContext.Current.User != null)
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                if (HttpContext.Current.User.Identity is FormsIdentity)
                {
                    // Get Forms Identity From Current User
                    FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
                    // Get Forms Ticket From Identity object
                    FormsAuthenticationTicket ticket = id.Ticket;
                    // userdata string was retrieved from stored user-data (a roles string from db "Users" table, e.g. "Admin;Manager;User")
                    string userData = ticket.UserData;
                    string[] roles = userData.Split(',');
                    // Create a new Generic Principal Instance and assign to Current User
                    HttpContext.Current.User = new GenericPrincipal(id, roles); // (could also be a custom principal object of your design)
                }
            }
        }
It works fine untill I call this at any other .aspx page:

if (!HttpContext.Current.User.IsInRole("Admin"))
            {
                Response.Redirect("Users.aspx");
            }

Even though the Admin group is set in the cookie the function returns false!

Whai I noticed is that at Global.asax the line of code
Code:
HttpContext.Current.User = new GenericPrincipal(id, roles);
sets the User to be of a class GenericPrincipal. However when calling IsInRole() at any other.aspx page the HttpContext.Current.User gets automatically transformed into RolesPrincipal?!

Any ideas on how to solve this?!

Thanks!

 
Old April 20th, 2007, 03:29 AM
Registered User
 
Join Date: Mar 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to bignermo
Default

It's me again...

Well I have solved the problem!

Here it is:

1) Once i worked with ASP.NET integrated role management system and in the global Web.config file the <roleManager enabled="true"...> was set.

2) Now I am working with custom made SQL DB User/Role management and had used Forms authentication.

3) The problem was that the .NET framework had automatically performed a cast from GenericPricipal to RolePricipal therefore dismissing all of the GenericPricipal's previously set roles!

I hope this will help somebody!

Bye...

 
Old August 8th, 2007, 11:51 AM
Registered User
 
Join Date: Aug 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks, bignermo!!! This helped me out a lot, and saved me a bunch of time! You are awesome!






Similar Threads
Thread Thread Starter Forum Replies Last Post
Is IsInRole change if changes to ActiveDirectory alyeng2000 ASP.NET 1.0 and 1.1 Professional 3 September 29th, 2004 02:20 PM
ISINROLE is not working alyeng2000 ASP.NET 1.0 and 1.1 Professional 0 August 20th, 2004 07:13 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.