Asked by:
userName is empty after successful login

Question
-
User76788328 posted
I login with username ans password, I am authenticated (User.Identity.Authenticated == True)
when I try to get the userName I get empty string
here is my code (userLevel.aspx.cs)
protected void Page_Load(object sender, EventArgs e) { if (!User.Identity.IsAuthenticated) { Response.Redirect("~/login.aspx?ReturnUrl=userLevel.aspx"); } if (!IsPostBack) { if (Request.Form["action"] == "getUserData") { string nm = User.Identity.Name; Response.Clear(); Response.Write(nm); Response.End(); } } }
in the immediate window I see (while in break point)
User.Identity {System.Web.Security.FormsIdentity} [System.Web.Security.FormsIdentity]: {System.Web.Security.FormsIdentity} AuthenticationType: "Forms" IsAuthenticated: true Name: "" Membership.GetUser() null Membership.GetUser("moria") {moria} Comment: null CreationDate: {23/02/2016 01:10:08} Email: "orders.bc@gmail.com" IsApproved: true IsLockedOut: false IsOnline: false LastActivityDate: {24/02/2016 03:21:08} LastLockoutDate: {01/01/1754 02:00:00} LastLoginDate: {24/02/2016 03:21:08} LastPasswordChangedDate: {23/02/2016 01:10:08} PasswordQuestion: "1" ProviderName: "MySqlMembershipProvider" ProviderUserKey: {ff589365-e852-4049-8803-6d12740414ee} UserName: "moria"
Wednesday, February 24, 2016 1:55 AM
All replies
-
User-1716253493 posted
Are you sure
Request.Form["action"] == "getUserData"
Wednesday, February 24, 2016 3:36 AM -
User-271186128 posted
Hi elic055,
Welcome to asp.net forum.
As oned_gk said, you could set break point to check the value of the "Request.Form["action"]".
Besides, you could try to remove the Request.Form["action"], and use the following code:
if (!IsPostBack) { string nm = User.Identity.Name; Response.Clear(); Response.Write(nm); Response.End(); }
Best regards,
DillionWednesday, February 24, 2016 7:50 AM -
User76788328 posted
OK, I will explain...
I use a jquery ajax to send data to the page, I want to get from the code behind details about the user based on his user name.
But, just for checking I put a server side textbox and a server side button. onClick event of the button I put data into the textbox
protected void btn_Click(object sender, EventArgs e) { tb1.Text = User.Identity.Name + " " + User.Identity.IsAuthenticated.ToString(); }
in the tb1 I get True (the userName is empty string!
Wednesday, February 24, 2016 9:06 AM -
User76788328 posted
I opened a new page
I put there only a textbox and a button
public partial class userLevel2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { checkit(); } else { checkit(); } } protected void btn_Click(object sender, EventArgs e) { checkit(); } private void checkit() { if (User.Identity.IsAuthenticated) { tb1.Text = User.Identity.Name; } else { tb1.Text = "not authenticated"; } } }
Html
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="userLevel2.aspx.cs" Inherits="userLevel2" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="tb1" runat="server"></asp:TextBox> <asp:Button ID="btn" runat="server" Text="ok" onClick="btn_Click"/> </div> </form> </body> </html>
and I get an empty textbox
before and after I click the button!
in my web.config
<configuration> <connectionStrings> <add name="machonchavayaCS" connectionString="Data Source=secret;Initial Catalog=secret;uid=administrator;Password=secret" providerName="System.Data.SqlClient" /> </connectionStrings> <system.web> <authentication mode="Forms"> <forms loginUrl="login.aspx" defaultUrl="machon.html" /> </authentication> <authorization> <deny users="?" /> </authorization> <membership defaultProvider="MySqlMembershipProvider" userIsOnlineTimeWindow="1"> <providers> <clear/> <add name="MySqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="machonchavayaCS" enablePasswordRetrieval="true" enablePasswordReset="true" requiresQuestionAndAnswer="false" applicationName="machonchavaya" requiresUniqueEmail="false" passwordFormat="Encrypted" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" passwordStrengthRegularExpression=""/> </providers> </membership> <customErrors mode="Off"/> <compilation targetFramework="4.0" debug="true"/> <pages enableEventValidation="false" viewStateEncryptionMode="Never"/> <machineKey validation="SHA1" validationKey="bla bla bla....." decryption="AES" decryptionKey="bla bla ...."/> </system.web> </configuration>
in additional page I have a greedVew with datasource that list all my users, and they are all there.
if you need any other piece of information that might solve the problem let me know, please.
Thursday, February 25, 2016 12:39 AM -
User-271186128 posted
Hi elic055,
According to your code, it seems that you are using Membership, instead of Asp.Net Identity. If that is the case, you could use Membership.GetUser Method to get the current user.
More details, see: https://msdn.microsoft.com/en-us/library/fcxcb339(v=vs.110).aspx
Best regards,
DillionThursday, February 25, 2016 8:17 AM -
User76788328 posted
I tried to get the userName using
Membership.GetUser().UserName
I got
An exception of type 'System.NullReferenceException' occurred in App_Web_r40at1ve.dll but was not handled in user code
Thursday, February 25, 2016 8:48 AM