RunWithElevatedPrivileges: ArgumentException - encodedValue


  • I have weird problem with SharePoint Foundation 2010.

    I have code that return SPWeb. The method gets the relevant SPSite and takes out the specific SPWeb by its name.

    GetWeb method is:

    public SPWeb GetWeb()
      SPWeb _web = null;
        _web = GetSite().AllWebs[SiteName];
        _web.AllowUnsafeUpdates = true;
      return _web;


    GetSite method is:

    public SPSite GetSite()
      SPSite _site = new SPSite(RepositoryUrl) { AllowUnsafeUpdates = true };
      return _site;


    The problem is that sometimes this is works great and sometimes I get an exception.

    The exception is:

    Exception of type 'System.ArgumentException' was thrown.

    Parameter name: encodedValue


    This is the StackTrace:

      at Microsoft.SharePoint.Administration.Claims.SPClaimEncodingManager.DecodeClaimFromFormsSuffix(String encodedValue)

       at Microsoft.SharePoint.Administration.Claims.SPClaimProviderManager.GetProviderUserKey(String encodedSuffix)

       at Microsoft.SharePoint.SPSecurity.GetCurrentUserTokenNoApplicationPrincipalDelegated()

       at Microsoft.SharePoint.SPSecurity.GetCurrentUserToken()

       at Microsoft.SharePoint.SPSecurity.EnsureOriginatingUserToken()

       at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(WaitCallback secureCode, Object param)

       at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(CodeToRunElevated secureCode)

       at StarLIMS.SharePoint.SharePointDal.GetWeb()


    What is that?


    I’m working with Windows server 2008 X64, Sharepoint Foundation 2010, Visual studio 2008 (C#), Sql Server 2008.




    Sunday, May 23, 2010 3:33 PM


  • Hello All, I am able to solve above said problem by simply setting LoginCreatedUser="false" property of User Creation Wizard.
    Monday, July 04, 2011 11:30 AM

All replies

  • Hi,

    I'm getting the same error when trying to authenticate user using FBA. Did you able to solve the problem?




    Monday, June 14, 2010 9:43 AM
  • It's saying the argument exception is being thrown on the siteName param.

    Also, your code is leaking memory. :( Both SPSite and SPWeb are disposable and should be wrapped in using statements.

    Daniel Larson, SharePoint MVP, MS Press Author, NewsGator Software Developer Check out my books on Amazon: - Inside Microsoft Windows SharePoint Services 3.0 (with Ted Pattson) - Developing Service Oriented AJAX Applications on the Microsoft Platform (.NET 3.5, WCF, Microsoft AJAX)
    • Proposed as answer by Darrin Bishop Wednesday, June 16, 2010 1:06 PM
    Monday, June 14, 2010 6:27 PM
  • I'm getting this very error when registering a new user in Sharepoint 2010 (FBA) using asp:CreateUserWizard. For already existing users, the FBA login process works just fine.

    When I run the registration.aspx page and input the username, pasword and email for the new user, the user gets created just fine and the wizard brings up the success page with the "Continue" button. Clicking on "Continue" brings up this error:

    [ArgumentException: Exception of type 'System.ArgumentException' was thrown.
    Parameter name: encodedValue]
       Microsoft.SharePoint.Administration.Claims.SPClaimEncodingManager.DecodeClaimFromFormsSuffix(String encodedValue) +622
       Microsoft.SharePoint.Administration.Claims.SPClaimProviderManager.GetProviderUserKey(String encodedSuffix) +73
       Microsoft.SharePoint.Utilities.SPUtility.GetFullUserKeyFromLoginName(String loginName) +10
       Microsoft.SharePoint.SPGlobal.CreateSPRequestAndSetIdentity(SPSite site, String name, Boolean bNotGlobalAdminCode, String strUrl, Boolean bNotAddToContext, Byte[] UserToken, String userName, Boolean bIgnoreTokenTimeout, Boolean bAsAnonymous) +3086
       Microsoft.SharePoint.SPWeb.InitializeSPRequest() +229
       Microsoft.SharePoint.WebControls.SPControl.EnsureSPWebRequest(SPWeb web) +375
       Microsoft.SharePoint.WebControls.SPControl.SPWebEnsureSPControl(HttpContext context) +541
       Microsoft.SharePoint.ApplicationRuntime.BaseApplication.Application_PreRequestHandlerExecute(Object sender, EventArgs e) +350
       Microsoft.SharePoint.ApplicationRuntime.SPRequestModule.PreRequestExecuteAppHandler(Object oSender, EventArgs ea) +59
       System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +80
       System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +171

    Any pointers will be much appreciated!

    Wednesday, June 16, 2010 8:02 PM
  • I am also getting this error.  I am logging in from a custom sign in page. 

    It authenticates the fba user
    Calls FormsAuthentication.SetAuthCookie(logon_userName.Text, False)
             FormsAuthentication.RedirectFromLoginPage(CStr(logon_userName.Text.Trim), False))

    But cannot redirect.  It seems it cannot get the source from the ReturnURL. 

    Login page:

    This error is in the event log:

    Request URL: http://<server>/_layouts/Authenticate.aspx?Source=An unhandled exception has occurred.F
    Request path: /_layouts/Authenticate.aspx
    User host address: ::1
    User: USERNAME
    Is authenticated: True
    Authentication Type: Forms

    Stack trace: at Microsoft.SharePoint.Administration.Claims.SPClaimEncodingManager.DecodeClaimFromFormsSuffix(String encodedValue)
    at Microsoft.SharePoint.Administration.Claims.SPClaimProviderManager.GetProviderUserKey(String encodedSuffix)

    Any thoughts?  Any direction is greatly appreciated.

    Thursday, June 24, 2010 4:05 PM
  • I am getting the exact same problem and have narrowed it down to the following:

    When I am logged in as a FBA authenticated user any calls to the [SPSecurity.RunWithElevatedPrivileges] method throws an exception however if I run the same code using an Anonymous Account it all works fine. It seems as an FBA user you cannot Elevate the Privileges, I have found numerous articles that mention setting the Current HttpContext to null however that does not work.


    Does anyone else have a solution?

    Wednesday, July 07, 2010 6:01 AM
  • Fixed it by setting the Current HttpContext to null outside of the delegate() method.


    HttpContext httpCxt = HttpContext.Current;

    HttpContext.Current = null;




    using (SPSite elevatedSite = new SPSite(SharePointURL))


    elevatedRootWeb = elevatedSite.RootWeb;




    HttpContext.Current = httpCxt;


    Wednesday, July 07, 2010 6:12 AM
  • I had the same problem but I did the following:


    protected void CreateUserWizard1_CreatedUserClear(object sender, EventArgs e)

    Which fixed it.

    Hope this helps you or someone else finding the same problem.

    Monday, July 26, 2010 8:13 AM
  • Thanks for posting the additional information everyone.

    We are gathering additional information on this and hoepfully will look to see if a fix is in order for this issue.


    Patrick Cole - MSFT
    Friday, August 06, 2010 6:24 PM
  • @ Patrick We are also having this problem which is killing our migration to SP 2010.

    Is MS releasing a fix for this?  We've replicated our old environment, and our development team consider this to be a bug.  This is one of the very last sticking points in our migration to the new platform.  We also integrate with the Microsoft Dynamics CRM, in fact its a CRM plugin we've written that's failing to open the sharepoint site.

    We *urgently* need assistance with this please or the business is likely to can the project.  That's not going to be good for anyone!

    I dont think setting the http context to null is an acceptable workaround, and I believe the dev's already tried that with no success.

    Is there a 'by request only' hotfix?  It certainly seems to be a bug with the 2010 code.

    Thanks for your time, an update would be greatly appreciated!!


    Exception: Unhandled Exception: System.ApplicationException: In execute : Where at : Doing run with extended privleges with delegate to site http://sharepoint/site Exception of type 'System.ArgumentException' was thrown.

    Parameter name: encodedValue

    Stack trace: at Microsoft.SharePoint.Administration.Claims.SPClaimEncodingManager.DecodeClaimFromFormsSuffix(String encodedValue)

    at Microsoft.SharePoint.Administration.Claims.SPClaimProviderManager.GetProviderUserKey(String encodedSuffix)


    Thursday, October 14, 2010 11:43 PM
  • Hello,

    I`m getting the same error on a Foundation Sharepoint.

    Where do I have to insert the code mentioned by jonno_76 or by sxkristian?


    Thx in advance!


    Tuesday, November 23, 2010 2:14 PM
  • Patrick,

    Is there any update on whether this is a bug or not? It's been a while since your post.  I am experiencing this same issue when adding a NEW FBA based user.  The user is created in the ASP.Net db ( I checked prior to clicking the Continue button on the CreateNewUser wizard) but once you click the Continue button this error occurs. 

    Additionally, you can log in with the new user on a new call to the SP site AFTER  the new user cookie expires or is deleted.  If one obserives the even log entries you will see that the new user is still used.  Also as an FYI...IIS and app pool resets will not necessarily clear this out.

    I tried the Request.Cookies.Clear(); but this did not help.


    Steve Kumbsky

    Steve Fraveling the Arvadent
    Monday, December 27, 2010 9:34 PM
  • Hi all,


    Is there any solution for this?


    Kind Regards

    Saturday, March 12, 2011 9:33 PM
  • I was getting this error after using the createUserWizard control. I fixed it with:

       Sub CreateUserWizard1_CreatedUser(ByVal s As Object, ByVal e As EventArgs)

            Microsoft.SharePoint.IdentityModel.SPClaimsUtility.AuthenticateFormsUser(Request.Url, CreateUserWizard1.UserName, CreateUserWizard1.Password)

    End Sub ' CreateUserWizard1_CreatedUser


    • Proposed as answer by A.N.T1 Monday, March 14, 2011 4:43 PM
    Monday, March 14, 2011 4:41 PM
  • I'm also intermittently getting the same error on our customtized login screen for SharePoint 2010 using claims based authentication (Windows & FBA). Does anyone know if there's an update on this from Microsoft?
    Wednesday, April 20, 2011 1:51 PM
  • You shouldn't be using your SPWeb object created inside the RunWithElevatedPrivileges outside the delegate. Its like begging for trouble.

    Just like you shouldn't use context from outside the delegate inside the delegate.


    Wednesday, April 20, 2011 2:00 PM
  • This error occurs when your site is configured for claims and you are logged into an account thats isn't being formatted as a proper claim.


    If your site is claims and you log in VIA Ad and the Welcome control says "Domain\SomeUser" that is not a properly encoded claim, so when the OOTB Claim Provider try's to decode the claim, it errors out with encodedValue.

    The code that processes that encoded value is looking for an "i"  before a "|" in the login name "Domain\UserName" and there isn't one, so it throws an error that the passed in encodedValue is invalid or null etc.

    This usually indicates that your web config files are not properly configured for a site running in claims,  or you could have FBA set up and that's not configured properly either.

    Refer to this link for that,

    In some cases, if your using extremely custom authentication logic "like a custom Trusted Identity Provider" then you need to write your own ClaimProvider so sharepoint knows how to encoded/decode the claims.

    When it's result and you log in Via a Windows account, your login name should look something like "i#:w|Domain\UserName" (*spelling*).  And when logged in via fba it should look like "i#:membershipprovidernamehere|usernamehere" (*spelling*)


    This error can also occur if you have your SuperReader and Cache Reader accounts configured with improper claim values.  When using a Claims Configuration, your SuperReader and CacheReader account names need to be Encoded Claims like the examples above.  And I bet this is what's happening in your example,

    When you try to get an SPSite, because you have the Cache Accounts configured, it's trying to check the cache to see if the Site exists in the Cache, but when it does it dies when trying to evaluate the SuperReader and SuperUser accounts because it can't decode their claims because their not valid encoded claims, then you get the encodedValue exception.  It works sometimes because the object might not be cached, so it gets a new one, then when the object is cached it dies.

    Or it might work when your logged in via AD but die when logged in via FBA etc (just a hypothetical theory).

    Heres an article on this issue as well. (I struggled with this for a good week before I found this).


    My Blog: Website: Under Construction
    Wednesday, April 20, 2011 3:23 PM
  • Sub CreateUserWizard1_CreatedUser(ByVal s As Object, ByVal e As EventArgs)
            Microsoft.SharePoint.IdentityModel.SPClaimsUtility.AuthenticateFormsUser(Request.Url, CreateUserWizard1.UserName, CreateUserWizard1.Password)
    End Sub ' CreateUserWizard1_CreatedUser
    suggested by A.N.T1 resolved the problem.
    Monday, April 25, 2011 10:09 PM
  • Hello All, I am able to solve above said problem by simply setting LoginCreatedUser="false" property of User Creation Wizard.
    Monday, July 04, 2011 11:30 AM
  • Shah Darshit, this worked for me, kind of makes sense, the login happens before the account has finished creating.


    "simply setting LoginCreatedUser="false" property of User Creation Wizard."



    Monday, January 16, 2012 10:20 AM
  • Hello,

    I also had a similar problem, after I created my custom login page and tried to open a page which had some ClientOM methods.

    I managed to fix it and posted the solution in my blog

    Hope it helps.

    Monday, April 02, 2012 11:14 AM