Index was outside the bounds of the array.

Posted on 2009-04-17
Last Modified: 2012-05-06
i am trying to get the username from active directory using the following code, the code works with no problem when in VS 2008 development server, but when i transfer it to our webserver
IIS 6 , windows 2003 , .net 3.5 SP1 ... it gives this error

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IndexOutOfRangeException: Index was outside the bounds of the array.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[IndexOutOfRangeException: Index was outside the bounds of the array.]
   _Default.Page_Load(Object sender, EventArgs e) +279
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
   System.Web.UI.Control.OnLoad(EventArgs e) +99
   System.Web.UI.Control.LoadRecursive() +50
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627

Version Information: Microsoft .NET Framework Version:2.0.50727.3082; ASP.NET Version:2.0.50727.3082

here is my code, very very simple

using System;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

using System.DirectoryServices;

using System.Security.Permissions;

using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page 


    protected void Page_Load(object sender, EventArgs e)


        String LDAPpath = "LDAP://";

        System.Security.Principal.WindowsIdentity wi = System.Security.Principal.WindowsIdentity.GetCurrent();

        string[] a = Context.User.Identity.Name.Split('\\');

        System.DirectoryServices.DirectoryEntry ADEntry = new System.DirectoryServices.DirectoryEntry("WinNT://" + a[0] + "/" + a[1]);

        string Name = ADEntry.Properties["FullName"].Value.ToString();

        Literal name = new Literal();

        name.Text = "Hello " + Name + ",";




Open in new window

Question by:Raul77
    LVL 32

    Accepted Solution

    Line 23, right?

    Context.User.Identity.Name is not returning something that is splitting on '\\' as expected.  You'll need to dump Context.User.Identity.Name and see what it's actually giving you.  If looking at it doesn't help, please post the result & I'll have a look too.


    Author Comment

    mmm you are right ... its blank !!! nothing is there .... but when i run it on my local machine its


    Author Comment

    ok i played with it, in IIS under Directory Security i un-checked anonymous login and checked Digest authentication
    now when i go to the site it ask for my user/pass when i put it it shows


    is there anyway it could do this without user entering user/pass ? as long as they are behind their computer in the domain, it shouldnt ask for user/pass. (thats my goal).


    Author Comment

    all working now, i had to re-start IIS and working now ...
    LVL 32

    Expert Comment

    by:Daniel Wilson
    >>is there anyway it could do this without user entering user/pass ? as long as they are behind their computer in the domain, it shouldnt ask for user/pass.

    Yeah, that's the other authentication type ... Integrated IIRC.

    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now