Portal Engine Questions on portal engine and web parts.
Version 5.x > Portal Engine > CMSContext.CurrentUser View modes: 
User avatar
Member
Member
smuhajir-hotmail - 10/26/2011 5:20:29 PM
   
CMSContext.CurrentUser
Hi,
I am trying to use windows identity foundation to authenticate a user. In doing so, it seems like Im coming across this piece of code that is throwing an error as noted below. Can you help me understand where CMSContext.CurrentUser is getting loaded from?
//Code from global.asax
if (RequestHelper.IsWindowsAuthentication() && UserInfoProvider.IsAuthenticated())
{
UserInfo currentUser = CMSContext.CurrentUser;
if (!currentUser.IsPublic())
{
UserInfoProvider.SetPreferredCultures
//Error message
Method is only supported if the user name parameter matches the user name in the current Windows Identity

Any help will be greatly appreciated,
Regards,
Shak

User avatar
Certified Developer 8
Certified Developer 8
Jiveabillion - 10/27/2011 11:32:42 AM
   
RE:CMSContext.CurrentUser
Have you added any role providers to your web.config?

User avatar
Member
Member
smuhajir-hotmail - 10/27/2011 11:41:11 AM
   
RE:CMSContext.CurrentUser
Hmmm... Nothing custom, still using the default role provider, CMSProvider.

User avatar
Certified Developer 8
Certified Developer 8
Jiveabillion - 10/27/2011 11:57:17 AM
   
RE:CMSContext.CurrentUser
The exception you are getting is thrown by System.Web.WinodwsTokenRoleProvider
probably in the private method GetCurrentWindowsIdentityAndCheckName where it checks to see if the UserName passed to that method is equal to HttpContext.Current.User.Identity.Name and throws that exception if it is not equal.

I do not know how to fix your issue, but you could write a code snippet to output the value of HttpContext.Current.User.Identity.Name and see if that exists as a UserName in your kentico site.

What it actually does is cast HttpContext.Current.User.Identity as WindowsIdentity, but it doesn't if the usernames do not match.


User avatar
Member
Member
smuhajir-hotmail - 10/27/2011 12:10:51 PM
   
RE:CMSContext.CurrentUser
I appreciate your repines on this. I actually figured that there is a mismatch somewhere so, I tried to output the username for the current page identity and it gave me a value of "David". Which is correct. Thats the user Im using to authenticate. This user also exists in the default users table in Kentico. I tried changing out a few users and adding them to the Kentico users table as well. But unfortunately haven't been able to get past this error.
In short, there is a user David in kentico and thats the identity when I response.write the authenticated user's name as well.
Maybe Im not matching the correct userID vs username vs name etc... I will look at it and report back.
Thanx again. Much Appreciated.

User avatar
Member
Member
smuhajir-hotmail - 10/27/2011 2:43:39 PM
   
RE:CMSContext.CurrentUser
So, here's an update. I put some response.writes in the global.asax files. Here are the results.

UserInfoProvider.GetUserName()) = david
HttpContext.Current.User.Identity.Name) = david
CMSContext.IsAuthenticated() = True

Using CMSContext.CurrentUser.UserName gives the following error:

Exception Details: System.Configuration.Provider.ProviderException: Method is only supported if the user name parameter matches the user name in the current Windows Identity.

So, I guess the question is, why is the CMSContext saying that the user is authenticated but doesn't really match the username/name or something?

Regards
Here's the stack trace:[ProviderException: Method is only supported if the user name parameter matches the user name in the current Windows Identity.]
System.Web.Security.WindowsTokenRoleProvider.GetCurrentWindowsIdentityAndCheckName(String userName) +3647658
System.Web.Security.WindowsTokenRoleProvider.GetCurrentTokenAndCheckName(String userName) +49
System.Web.Security.WindowsTokenRoleProvider.GetRolesForUser(String username) +143
CMS.SiteProvider.UserInfoProvider.AuthenticateUserWindows(IPrincipal user, String siteName) +187
CMS.CMSHelper.CMSContext.GetCurrentUser(Boolean& loadUserData) +403
CMS.CMSHelper.CMSContext.get_CurrentUser() +24
Global.Session_Start(Object sender, EventArgs e) in c:\inetpub\wwwroot\KenticoCMS\App_Code\Global.asax.cs:216
System.Web.SessionState.SessionStateModule.CompleteAcquireState() +409
System.Web.SessionState.SessionStateModule.BeginAcquireState(Object source, EventArgs e, AsyncCallback cb, Object extraData) +1296
System.Web.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +115
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +371

User avatar
Kentico Support
Kentico Support
kentico_zdenekc - 11/16/2011 3:17:19 AM
   
RE:CMSContext.CurrentUser
Hello,

I'd like to add some comments:

CMSContext.IsAuthenticated()
basically performs

HttpContext.Current.User.Identity.IsAuthenticated

and following goes into the AuthenticateUserWindows() method:

UserInfoProvider.AuthenticateUserWindows(HttpContext.Current.User, CurrentSiteName);

It may be possible there was some issue earlier... which version are you using?

Regards,
Zdenek C.

User avatar
Member
Member
ryan.graham-ddiworld - 7/24/2012 1:24:11 PM
   
RE:CMSContext.CurrentUser
Hi smuhajir,

Were you ever able to get this working? I'm running into the same problem integrating Kentico with WIF.

Thanks in advance!
Ryan

User avatar
Member
Member
smuhajir-hotmail - 7/24/2012 2:09:09 PM
   
RE:CMSContext.CurrentUser
Ryan,

Yes I was able to get it working with a not so elegant work around. What I noticed was that the cmscontext was getting reset to something else in quite a few different methods. It was getting reloaded to admin user or a default user.
What I ended up doing was searching for each line of code that was referencing CMSContext.CurrentUser and adding a line of code above this to take the identity of hte user from teh httpcontext and setting it to cmscontext.
its been a while since I did this, so I dont have exact code fragments at hand. But if you need more info, pls ping me back and I can copy and paste a few examples. But from what I remember now, it was just a matter of setting hte cmscontext of the current user to be the same as httpcontext of the user. HTTP context was being set up correctly by wif and didnt change. The answers above helped me understand what was going on. So, once again, thank you for that.

Regards,
Shak