问题描述
第一次发帖求助,大家帮帮忙。现在我要从AD中读取当前登录的用户名,并且将其显示在页面上。目前我们写了一个方法如下:==================================privatestringGetName(){//获取当前登陆用户的登陆名WindowsPrincipalwp=(WindowsPrincipal)System.Threading.Thread.CurrentPrincipal;stringwpname=wp.Identity.Name.ToString();intj=wpname.LastIndexOf("\");stringuserName=wpname.Substring(j+1);stringdomainName=wpname.Substring(0,j);//Session["user"]=userName.ToString();//获得当前用户名//Session["domain"]=domainName.ToString();//获得当前域名returnuserName;}==================================然后在页面上有一个Lable用来读取这个得到的用户名:==================================lblName.Text=this.GetName();==================================这个程序在调试中能够正常显示,但是一旦我将其发布,就会出错了。下面是这个网站的完整代码:首先是后台文件:==================================usingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Web.UI.HtmlControls;usingSystem.Security.Principal;usingSystem.Globalization;publicpartialclass_Default:System.Web.UI.Page{protectedvoidPage_Load(objectsender,EventArgse){if(!IsPostBack){lblName.Text=this.GetName();}}privatestringGetName(){//获取当前登陆用户的登陆名WindowsPrincipalwp=(WindowsPrincipal)System.Threading.Thread.CurrentPrincipal;stringwpname=wp.Identity.Name.ToString();intj=wpname.LastIndexOf("\");stringuserName=wpname.Substring(j+1);stringdomainName=wpname.Substring(0,j);//Session["user"]=userName.ToString();//获得当前用户名//Session["domain"]=domainName.ToString();//获得当前域名returnuserName;}}==================================然后是前台显示文件:==================================<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default"%><!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><headrunat="server"><title>无标题页</title></head><body><formid="form1"runat="server"><div><asp:LabelID="lblName"runat="server"Text="lblName"></asp:Label></div></form></body></html>==================================发布成功后,在网页中浏览会出现如下错误:==================================ServerErrorin'/'Application.--------------------------------------------------------------------------------Lengthcannotbelessthanzero.Parametername:lengthDescription:Anunhandledexceptionoccurredduringtheexecutionofthecurrentwebrequest.Pleasereviewthestacktraceformoreinformationabouttheerrorandwhereitoriginatedinthecode.ExceptionDetails:System.ArgumentOutOfRangeException:Lengthcannotbelessthanzero.Parametername:lengthSourceError:Anunhandledexceptionwasgeneratedduringtheexecutionofthecurrentwebrequest.Informationregardingtheoriginandlocationoftheexceptioncanbeidentifiedusingtheexceptionstacktracebelow.StackTrace:[ArgumentOutOfRangeException:Lengthcannotbelessthanzero.Parametername:length]System.String.InternalSubStringWithChecks(Int32startIndex,Int32length,BooleanfAlwaysCopy)+2819889_Default.GetName()+120_Default.Page_Load(Objectsender,EventArgse)+28System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtrfp,Objecto,Objectt,EventArgse)+15System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Objectsender,EventArgse)+34System.Web.UI.Control.OnLoad(EventArgse)+99System.Web.UI.Control.LoadRecursive()+47System.Web.UI.Page.ProcessRequestMain(BooleanincludeStagesBeforeAsyncPoint,BooleanincludeStagesAfterAsyncPoint)+1061--------------------------------------------------------------------------------VersionInformation:Microsoft.NETFrameworkVersion:2.0.50727.832;ASP.NETVersion:2.0.50727.832==================================
解决方案
解决方案二:
各位大侠帮帮忙啊……实在搞不定这个问题。在调试的时候能够看到,都好好的等到发布了之后,再在网页中浏览就会出错了……
解决方案三:
哪位高手看看啊?