Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 20th, 2007, 02:59 AM
Registered User
 
Join Date: Mar 2007
Location: Sarajevo, Bosnia, Bosnia and Herzegovina.
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!

  #2 (permalink)  
Old April 20th, 2007, 03:29 AM
Registered User
 
Join Date: Mar 2007
Location: Sarajevo, Bosnia, Bosnia and Herzegovina.
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...

  #3 (permalink)  
Old August 8th, 2007, 11:51 AM
Registered User
 
Join Date: Aug 2007
Location: , , .
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!

 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 03:17 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.