YAFLogo

ignition22
  • ignition22
  • 51.8% (Neutral)
  • YAF Forumling Topic Starter
15 years ago
Hey Guys

I'm currently working on a solution combining a web application with YAF Forums 1.9.3.

Im planning to use the security and login functionality of the YAF forums for both YAF and the original site however i have come across problems trying to get hold of the user from the original site once logged into YAF.

I find that i can use Role providers from my main site without an issue ie

return Roles.IsUserInRole("Administrators");

however when i try to use

MembershipUser user = Membership.GetUser();

It will always throw an object reference exception. I imagine it is because it cannot find a current user. It seems strange to me then that this very command is used in YAF without any problem. Perhaps the main site gets the same instance of the Role Provider but when it access the Membership Provider it gets a different instance ??

Can someone let me know, is this typically how you should go about getting the current user? If i at least know that then i have something to go on however if that is incorrect it would be good to know before going down that road any further.

Any help is greatly appreciated

Cheers

Sponsor
mddubs
  • mddubs
  • 100% (Exalted)
  • YAF Developer
15 years ago
http://msdn.microsoft.com/en-us/library/system.web.security.membership.getuser.aspx 

I don't usually see that method called without any parameters so I had to check with the above link. It looks like that will return the current user if, of course, you're using the .NET membership framework. So my only guess is you did not setup YAF to use the .net membership schema and that's why that call fails.


UserPostedImage 

www.bunkerhollow.com  | www.careercomputing.com 

When I post fp:mddubs in a topic, I'm leaving my footprint there so I can track it once I get into coding/supporting. (Yes I stole this off Mek 🙂, who stole this off Ederon 🙂 )

jshepler
15 years ago

Can someone let me know, is this typically how you should go about getting the current user?

ignition22 wrote:

Yes, it is typically how you should go about getting the current user IF the site uses the asp.net membership framework.

As to why you're getting an exception, it's pretty much impossible to tell without seeing the entire exception including stack trace. In my experience, there are times when the exception will show a line of code as the source of the exception, but is not where the exception is actually coming from. The stack trace, however, never lies.

Also, I believe that GetUser() will return a null if the user is not authenticated (anonymous). The documentation doesn't specify this, but I'm pretty sure this is true based on my experience using it.


not jsheLPer

ignition22
  • ignition22
  • 51.8% (Neutral)
  • YAF Forumling Topic Starter
15 years ago
oops someone just broke the forums haha

As to why you're getting an exception, it's pretty much impossible to tell without seeing the entire exception including stack trace.

jshepler wrote:

Your on the money there, should have looked at the evidence before thinking to logically about the problem 🙂. The way things are running at the moment the main site and forums each have their own webconfig.

The trace was pointing to YAF.Classes.Config.get_ConnectionString() which led me to discover that it was looking for a connection string named "yafnet" where as i had renamed the connection string in the main site and thought i had also updated all references some time ago.

because GetUser() was being called in the instance from the main site instead of the forum site it was looking for an entry in its own config.

I think because Roles.IsUserInRole might access session variables or something other then the database on every call it wasnt causing the same problem

Cheers guys