获取当前登录用户

时间:2011-05-21 23:33:05

标签: asp.net

我正在使用Activedirectory作为会员提供商。我想将当前登录的用户存储在会话中。

 protected void user_login_LoggedIn(object sender, EventArgs e)
 {
   MembershipUser user = Membership.GetUser();
 }

上面的代码给出了以下错误


The parameter 'username' must not be empty.
Parameter name: username 

    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. 

异常详细信息:

System.ArgumentException: The parameter 'username' must not be empty.
Parameter name: username

来源错误:

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.

堆栈追踪:

[ArgumentException: The parameter 'username' must not be empty.
Parameter name: username]
   System.Web.Util.SecUtility.CheckParameter(String& param, Boolean checkForNull, Boolean checkIfEmpty, Boolean checkForCommas, Int32 maxSize, String paramName) +1976411
   System.Web.Security.ActiveDirectoryMembershipProvider.CheckUserName(String& username, Int32 maxSize, String paramName) +30
   System.Web.Security.ActiveDirectoryMembershipProvider.GetUser(String username, Boolean userIsOnline) +86
   System.Web.Security.Membership.GetUser(String username, Boolean userIsOnline) +82
   System.Web.Security.Membership.GetUser() +19
   darts.login.user_login_LoggedIn(Object sender, EventArgs e) in E:\Projects\DARTS\source\darts\darts\login.aspx.cs:24
   System.Web.UI.WebControls.Login.OnLoggedIn(EventArgs e) +111
   System.Web.UI.WebControls.Login.AttemptLogin() +179
   System.Web.UI.WebControls.Login.OnBubbleEvent(Object source, EventArgs e) +101
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
   System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs e) +118
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +166
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565

版本信息:Microsoft .NET Framework版本:2.0.50727.4211; ASP

1 个答案:

答案 0 :(得分:0)

编辑:

基于在ASP.NET中使用它来获取有关当前经过身份验证的用户的登录信息,我看到了一些可行的方法。首先,如果您使用Active Directory成员资格提供程序进行Forms身份验证,则可以在那里捕获用户名。然后你只需将用户名放在GetUser()方法中,如下所示:GetUser(yourUser),你应该得到所需的信息。

如果这不是一个选项,我相信用户名信息存储在您的登录控件下(在您的情况下,如果您确实使用登录控件,我相信名称为user_login)。您只需说出user_login.username并将其放在GetUser()方法中。

最后,您可以尝试HttpContext.Current.User.Identity.Name的旧回退并将其放在GetUser()方法中。