When a user is created in SharePoint 2013 (using forms based authentication) the UserToken is not set for the user.

When the users token then is accessed programmatically a "OutOfMemoryException" is thrown. In our case it happens when we try to use SPWeb.GetUserEffectivePermissions(loginName).

This happens until the user visits the site and tp_ExternalToken etc. is set.

The question is, how can I force SharePoint to do this before the first logon?

Relevant part of stack trace:

Type : System.OutOfMemoryException, mscorlib, Version=,   
Culture=neutral, PublicKeyToken=b77a5c561934e089
Message : Server Out Of Memory.

There is no memory on the server to run your program. Please contact your administrator with this problem.
Source : 
Help link : 
Data : System.Collections.ListDictionaryInternal
TargetSite : Byte[] GetUserToken(System.String, System.String)
HResult : -2147024882
Stack Trace :
    at Microsoft.SharePoint.Library.SPRequestInternalClass.GetUserToken(String bstrUrl, String bstrLogin)
    at Microsoft.SharePoint.Library.SPRequest.GetUserToken(String bstrUrl, String bstrLogin)
    at Microsoft.SharePoint.Utilities.SPUtility.GetPermissions(String login, SPWeb web, SPSecurableObject securableObject, Boolean useCache)
  • I've noticed that it starts working when the user logs in but only a while after the user logs out again. – nimatt Feb 17 '16 at 15:51

After a lot of digging I found a way to get the token set. It is not pretty but it is the only way I found...

var spClaim = SPClaimProviderManager.Local.ConvertSPUserToClaim(user);
var identity = new ClaimsIdentity();
identity .Claims.Add(new Claim(ClaimTypes.Name, user.LoginName.Substring(user.LoginName.IndexOf(':') + 1), ClaimValueTypes.String));
identity .Claims.Add(new Claim(spClaim.ClaimType, spClaim.Value, spClaim.ValueType, spClaim.OriginalIssuer, spClaim.OriginalIssuer));
SPUserToken token = new SPUserToken(identity , new Uri(Web.Url));
using (SPSite impSite = new SPSite(Web.Site.ID, token))
using (impSite.OpenWeb(impSite.RootWeb.ID))
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.