Michael's profileMike HackerBlogLists Tools Help

Blog


    21 August

    SharePoint User Profile Access Issues

    Today I will feature a problem recently encountered with updating and reading user profile information when connected to SharePoint as an anonymous user.

    The Task
    …create a custom login page for forms based authentication that validates specific user profile information prior to allowing the user to log into SharePoint.  In this instance a user would enter in their usename and password and then click on the login button.    The button’s click event will launch a method that validates user profile information for the specified username prior to completing the actual log in process.   This is needed to make sure a user has read and agreed to the site terms of service prior to be given any access to the system.   A simple user profile field that is not editable directly by the user holds the flag that indicates if the user has agreed to the terms of service.   If the user has previously agreed to the terms then the log in process completes.  If the user has not previously agree to the terms then the terms are displayed and the user has a chance to agree and login or disagree and leave the site.

    The Problem
    I need to read and update user profile information even though the user has yet to log into SharePoint through forms authentication.   The initial thought was to use SPSecurity.RunWithElevatedPriviliges to provide the elevated permissions needed to read and write to the profile store.    Elevated permissions are required because anonymous users do not have any permissions on the profile store.

    During testing of this solution I still continued to receive access denied errors when the exists method of the UserProfileManager object was executed.  This made no sense since the application pool account had full permissions to make modification to the user profile store.   (Note: when using RunWithElevatedPriviliges under a forms authenticated site the actual account used is the application pool account).

    The Cause
    With help from Lutz Roeder’s .NET Reflector utility I was able to peak into the actual SharePoint assemblies and view what was going on when I was calling the exists method on the UserProfileManager object.   I eventually tracked down the exact piece of code that was causing the access denied error.   It was in a static method called GetCurrentUserName in the UserProfileGlobal class.

    Since this class is protected under Microsoft copyright I will not be reproducing the actual code but I will explain at a high level what the GetCurrentUserName method does and why it caused the access denied errors. 

    The GetCurrentUserName class inspects the HttpContext.Current property and if it is not null it uses it to determine the current user and validates that the user can be authenticated and then returns the username.    If the HttpContext.Current property is null then the current WindowsIdentity is used to determine the current user and then returns that username.

    So back to our problem… the access denied error.    When using RunWithElevatedPriviliges impersonation takes place and the WindowsIdentity object now returns the SharePoint system account (which in our case is the application pool account).   RunWithElevatedPriviliges does not modify the HttpContext, so in our case since our user is not authenticated the HttpContext.Current.User property is set to an empty string.  This means that when the GetCurrentUserName method from the UserProfileGlobal class executes it sees the the HttpContext.Current property is not null and then attempts to validate the username which is an empty string.   This causes the method to return an access denied error.

    The Workaround
    Because our HttpContext.Current object is not null we keep falling into the part of the GetCurrentUserName method that tries to validate the username what happens to be an empty string because we do not have any user logged in yet.   What we really want is the HttpContext object to be null so that the method instead uses the SharePoint system account which does have full permissions over the user profile store.    If we just set HttpContext.Current = null we would cause the login page to no longer work.   This is because ASP.NET needs the HttpContext in order to properly process and render a web page.

    To prevent the ASP.NET page from no longer working it is important to store the value of the current context in a variable prior to setting the current context to null so that way it can be restored when done reading or writing from the user profile store.   Below is an example.

     

    HttpContext myContext = HttpContext.Current;
    HttpContext.Current = null;
    

    …do profile read / writes here …

    HttpContext.Current = myContext;

    Remember that the above code is run within a RunWithElevatedPriviliges code block.   It is also important to make sure you do not try to use any objects or methods that need access to the HttpContext object between the time you set the current context to null and when you restore it to the original value.

    Results
    So far this work around appears to be performing very well in a development environment.   If you do use this please be aware that future changes to the SharePoint API through service packs, hotfixes or other updates may cause code relying on this work around to no longer function as intended.   In other words, use at your own risk! 

    Comments (8)

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    ji zhiwrote:
    http://www.360batteries.com/suc-c3-usb-data-charging-cable-for-samsung-i8-i80-m110-sl310-sl310-w-tl9.html
    usb charging cable i80 i8 m110

    http://www.360batteries.com/suc-c3-usb-data-charging-cable-for-samsung-i8-i80-m110-sl310-sl310-w-tl9.html

    charging cable samsung sl310 i80

    http://www.360batteries.com/suc-c3-usb-data-charging-cable-for-samsung-i8-i80-m110-sl310-sl310-w-tl9.html

    usb data cable samsung
    http://www.360batteries.com/suc-c3-usb-data-charging-cable-for-samsung-i8-i80-m110-sl310-sl310-w-tl9.html

    samsung data cable i8 i80

    http://www.360batteries.com/suc-c3-usb-data-charging-cable-for-samsung-i8-i80-m110-sl310-sl310-w-tl9.html

    i8 i80 m110 sl310 usb charging data cable

    http://www.360batteries.com/suc-c3-usb-data-charging-cable-for-samsung-i8-i80-m110-sl310-sl310-w-tl9.html

    sl310 usb charging data cable


    http://www.360batteries.com/new-dell-latitude-d510-laptop-cpu-cooling-fan-n8715.html cpu cooling fan dell

    http://www.360batteries.com/new-dell-latitude-d510-laptop-cpu-cooling-fan-n8715.html dell d510 cpu cooling fan

    http://www.360batteries.com/new-dell-latitude-d510-laptop-cpu-cooling-fan-n8715.html dell laptop cpu fan

    http://www.360batteries.com/new-dell-latitude-d510-laptop-cpu-cooling-fan-n8715.html d510 cpu cooling fan

    http://www.360batteries.com/new-dell-latitude-d510-laptop-cpu-cooling-fan-n8715.html laptop d510 dell cpu fan

    http://www.360batteries.com/new-dell-latitude-d510-laptop-cpu-cooling-fan-n8715.html dell latitude cpu cooling fan



    http://www.360batteries.com/dell-latitude-e4200-e4300-xt-pp12s-ac-adapter-charger.html dell latitude ac adapter

    http://www.360batteries.com/dell-latitude-e4200-e4300-xt-pp12s-ac-adapter-charger.html ac adapter xt pp12s dell

    http://www.360batteries.com/dell-latitude-e4200-e4300-xt-pp12s-ac-adapter-charger.html e4200 e4300 ac adapter dell

    http://www.360batteries.com/dell-latitude-e4200-e4300-xt-pp12s-ac-adapter-charger.html dell e4300 pt ac adapter

    http://www.360batteries.com/dell-latitude-e4200-e4300-xt-pp12s-ac-adapter-charger.html latitude dell e4300 ac adapter

    http://www.360batteries.com/dell-latitude-e4200-e4300-xt-pp12s-ac-adapter-charger.html dell latitude pp12s ac adapter


    http://www.360batteries.com/video-cable-vc-100-for-canon-eos-5d-10d-20d-d30-d60-1d-mark-ii-1ds-mark-ii-1d.html

    canon video cable vc-100

    http://www.360batteries.com/video-cable-vc-100-for-canon-eos-5d-10d-20d-d30-d60-1d-mark-ii-1ds-mark-ii-1d.html

    canon EOS 5d 10d 20d video cable vc-100

    http://www.360batteries.com/video-cable-vc-100-for-canon-eos-5d-10d-20d-d30-d60-1d-mark-ii-1ds-mark-ii-1d.html

    eos d30 d60 video canon cable

    http://www.360batteries.com/video-cable-vc-100-for-canon-eos-5d-10d-20d-d30-d60-1d-mark-ii-1ds-mark-ii-1d.html

    d60 d30 video canon cable vc-100

    http://www.360batteries.com/video-cable-vc-100-for-canon-eos-5d-10d-20d-d30-d60-1d-mark-ii-1ds-mark-ii-1d.html

    vc-100 video cable canon

    http://www.360batteries.com/video-cable-vc-100-for-canon-eos-5d-10d-20d-d30-d60-1d-mark-ii-1ds-mark-ii-1d.html

    20d d60 video cable for canon


    http://www.360batteries.com/hp-pavilion-dv6000-dv6100-laptop-cpu-fan-heatsink-431450-001.html hp cpu cooling fan

    http://www.360batteries.com/hp-pavilion-dv6000-dv6100-laptop-cpu-fan-heatsink-431450-001.html dv6000 cpu cooling fan

    http://www.360batteries.com/hp-pavilion-dv6000-dv6100-laptop-cpu-fan-heatsink-431450-001.html cpu cooling fan hp dv6100

    http://www.360batteries.com/hp-pavilion-dv6000-dv6100-laptop-cpu-fan-heatsink-431450-001.html laptop pavilion cpu fan

    http://www.360batteries.com/hp-pavilion-dv6000-dv6100-laptop-cpu-fan-heatsink-431450-001.html pavilion dv6000 cpu fan

    http://www.360batteries.com/hp-pavilion-dv6000-dv6100-laptop-cpu-fan-heatsink-431450-001.html cpu cooling fan dv6000
    4 days ago
    4 Nov.
    No namewrote:
    http://www.toplaptopbatteries.com/dell/pu563.htm dell pu563 battery
    http://www.toplaptopbatteries.com/hp/v3000.htm hp v3000 battery
    http://www.toplaptopbatteries.com/hp/v6000.htm hp v6000 battery
    http://www.toplaptopbatteries.com/dell/latitude-c810.htm dell latitude c810 battery
    http://www.toplaptopbatteries.com/dell/latitude-cpi.htm dell latitude cpi battery
    http://www.toplaptopbatteries.com/dell/latitude-cpx.htm dell latitude cpx battery
    http://www.toplaptopbatteries.com/dell/inspiron-2500.htm dell inspiron 2500 battery
    http://www.toplaptopbatteries.com/compaq/nx9010.htm compaq nx9010 battery
    http://www.toplaptopbatteries.com/apple/a1022.htm apple a1022 battery
    http://www.toplaptopbatteries.com/apple/m9324.htm apple m9324 battery
    http://www.toplaptopbatteries.com/sony/12-cell-vgp-bps2a.htm sony 12-cell vgp-bps2a battery
    http://www.toplaptopbatteries.com/sony/12-cell-vgp-bps2b.htm sony 12-cell vgp-bps2b battery
    http://www.toplaptopbatteries.com/sony/12-cell-vgp-bps2.htm sony 12-cell vgp-bps2 battery
    http://www.toplaptopbatteries.com/ibm/thinkpad-a30.htm ibm thinkpad a30 battery
    http://www.toplaptopbatteries.com/apple/powerbook-g4.htm apple powerbook g4 battery
    http://www.toplaptopbatteries.com/apple/m8244g-b.htm apple m8244g b battery
    http://www.toplaptopbatteries.com/dell/inspiron-3800.htm dell inspiron 3800 battery
    http://www.toplaptopbatteries.com/ibm/thinkpad-t40p.htm ibm thinkpad t40p battery
    http://www.toplaptopbatteries.com/ibm/thinkpad-t41p.htm ibm thinkpad t41p battery
    http://www.toplaptopbatteries.com/ibm/thinkpad-t42p.htm ibm thinkpad t42p battery
    http://www.toplaptopbatteries.com/ibm/thinkpad-t43p.htm ibm thinkpad t43p battery
    http://www.toplaptopbatteries.com/acer/black-aspire-one-a110l-series.htm acer black aspire one a110l series battery
    http://www.toplaptopbatteries.com/acer/black-aspire-one-a150l-series.htm acer black aspire one a150l series battery
    http://www.toplaptopbatteries.com/acer/black-aspire-one-a150x-series.htm acer black aspire one a150x series battery
    http://www.toplaptopbatteries.com/acer/blue-aspire-one-a110l-series.htm acer blue aspire one a110l series battery
    http://www.toplaptopbatteries.com/acer/blue-aspire-one-a150l-series.htm acer blue aspire one a150l series battery
    http://www.toplaptopbatteries.com/acer/blue-aspire-one-a150x-series.htm acer blue aspire one a150x series battery
    http://www.toplaptopbatteries.com/acer/um08a73.htm acer um08a73 battery
    http://www.toplaptopbatteries.com/acer/um08a74.htm acer um08a74 battery
    http://www.toplaptopbatteries.com/gateway/s62044l.htm gateway s62044l battery
    http://www.toplaptopbatteries.com/gateway/m325.htm gateway m325 battery
    http://www.toplaptopbatteries.com/gateway/s62066l.htm gateway s62066l battery
    http://www.toplaptopbatteries.com/gateway/bat0016.htm gateway bat0016 battery
    http://www.toplaptopbatteries.com/asus/a42-a3.htm asus a42-a3 battery
    http://www.toplaptopbatteries.com/asus/a3000.htm asus a3000 battery
    http://www.toplaptopbatteries.com/asus/z9100.htm asus z9100 battery
    http://www.toplaptopbatteries.com/asus/a3.htm asus a3 battery
    http://www.toplaptopbatteries.com/hp/383968-001.htm hp 383968-001 battery
    http://www.toplaptopbatteries.com/asus/z91.htm asus z91 battery
    http://www.toplaptopbatteries.com/sony/vgp-bps5.htm sony vgp-bps5 battery
    http://www.toplaptopbatteries.com/sony/vgp-bps5a.htm sony vgp-bps5a battery
    http://www.toplaptopbatteries.com/asus/a42-w3.htm asus a42-w3 battery
    http://www.toplaptopbatteries.com/asus/w3v.htm asus w3v battery
    http://www.toplaptopbatteries.com/asus/w3000.htm asus w3000 battery
    http://www.toplaptopbatteries.com/asus/a32-f3.htm asus a32-f3 battery
    1 Nov.
    Thanks Michael. Got me out of a sticky situation. It's a bit of a hack, but seems to work well.
    Randy - http://sharepointhawaii/randywilliams
    16 June
    No namewrote:
    You just solved a week long problem I was having.

    Thank you!
    30 Apr.
    You saved my day
    6 Feb.
    Thank you very much! I posted about you in my portuguese blog http://dgbsharepoint.wordpress.com.
    Your workaround is function very well so far.
    1 Feb.
    Matthiaswrote:
    You might just have saved my life. Thanks heaps for this article! :)
    11 Dec.

    Trackbacks

    Weblogs that reference this entry
    • None