Index was outside the bounds of the array.

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://192.168.0.2/";
        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 + ",";
        PlaceHolder1.Controls.Add(name);
    }
}

Open in new window

Raul77Asked:
Who is Participating?
 
Daniel WilsonConnect With a Mentor Commented:
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.


0
 
Raul77Author Commented:
mmm you are right ... its blank !!! nothing is there .... but when i run it on my local machine its
Domain\username

????
0
 
Raul77Author Commented:
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

Domain\username

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).

Thanks,
0
 
Raul77Author Commented:
all working now, i had to re-start IIS and working now ...
0
 
Daniel WilsonCommented:
>>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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.