Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

NullReferenceException when using custom role provider

Status
Not open for further replies.

BFarley

Programmer
May 22, 2008
43
US
I have a custom role provider & custom membership provider on my app.

Membership works okay so far, but I get an error when I try to use the role provider.

I've verified that the username (tcallahan) is in the database, and a member of the role ("Administrators") that I'm checking.

Here's the code that throws an error:

Code:
MsgBox(Roles.IsUserInRole("tcallahan", "Administrator"))

Full exception code:
Code:
System.NullReferenceException was unhandled by user code
  Message="Object reference not set to an instance of an object."
  Source="System.Web"
  StackTrace:
       at System.Web.Security.RolePrincipal.IsInRole(String role)
       at System.Web.Security.Roles.IsUserInRole(String username, String roleName)
       at scottsdale._Default.Button1_Click(Object sender, EventArgs e) in C:\Documents and Settings\myusername\My Documents\Visual Studio 2008\Projects\tucson\scottsdale\Default.aspx.vb:line 13
       at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
       at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException:

Thanks in advance for any guidance.

Bryant Farley

"The Dude Abides
 
start here
Code:
C:\Documents and Settings\myusername\My Documents\Visual Studio 2008\Projects\tucson\scottsdale\Default.aspx.vb:line 13
there is an object which has not been initialized.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Default.aspx.vb:line 13 refers to the line I posted originally

Code:
MsgBox(Roles.IsUserInRole("tcallahan", "Administrator"))

Bryant Farley

"The Dude Abides
 
A good start would be to use the debugger and find which object is null. And then remove the MsgBox which is pretty useless in a web application.

Mark,

[URL unfurl="true"]http://lessthandot.com[/url] - Experts, Information, Ideas & Knowledge
[URL unfurl="true"]http://mdssolutions.co.uk[/url] - Website Design
[URL unfurl="true"]http://aspnetlibrary.com[/url] - An online resource for professional ASP.NET developers
 
Roles or an object within IsUserInRole has not been instantiated.

MsgBox doesn't work in web apps because the code is executed on the server, not the client. if this works during development it's because your server and client are the same box. but I would never use this object anywhere in code to prevent myself from incidentally posting it to production.

plus if I need to see what values are being passed around I use a logging library like log4net and forgo any need to display messages while the application is executing.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Solved - in an unexpected way.

My role provider inherited SqlRoleProvider. For IsUserInRole, I used a modified version of the stored procedure aspnet_UsersInRoles_IsUserInRole, as I thought that the role provider would call it.

However, for IsUserInRole, it did not: instead, GetRolesForUser was called, and then the determination of user being in the specified role happened.

I discovered this by creating a role provider that inherited from RoleProvider, and throwing an exception for anything other than IsUserInRole.

Afterward, I modified my custom SqlRoleProvider and it works perfectly now.

So that's the story there.

Thanks to all who contributed ideas.

Bryant Farley

"The Dude Abides
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top