locked
Context.User.Identity.GetUserName() RRS feed

  • Question

  • User-1066344940 posted

    Hi All,

    I have a ASP.Net Web Forms application. I am using EF code first, with Model Binding. Everything is going good. I am ready to start incorporating Authentication. I am using the out of the box Membership Provider. I pointed the Membership provider to my existing Database. All went well, everything was created,  ie. User table, Role table, etc. I have a LoginView control that I am using:

    <asp:LoginView ID="LoginView1" runat="server" ViewStateMode="Disabled">
                            <AnonymousTemplate>
                                <ul class="nav navbar-nav navbar-right">
                                    <li><a id="A5" runat="server" href="~/Account/Register">Register</a></li>
                                    <li><a id="A6" runat="server" href="~/Account/Login">Log in</a></li>
                                </ul>
                            </AnonymousTemplate>
                            <LoggedInTemplate>
                                <ul class="nav navbar-nav navbar-right">
                                    <li><a id="A7" runat="server" href="~/Account/Manage" title="Manage your account">Hello, <%:Context.User.Identity.GetUserName() %> !</a></li>
                                    <li>
                                        <asp:LoginStatus ID="LoginStatus1" runat="server" LogoutAction="Redirect" LogoutText="Log off" LogoutPageUrl="~/" OnLoggingOut="Unnamed_LoggingOut" />
                                    </li>
                                </ul>
                            </LoggedInTemplate>
                        </asp:LoginView>

    I am getting an error on the, GetUserName() method? It is telling me that

    System.Security.Principal.Identity;

    Does not contain a definition for GetUserName(). I have the proper namespaces referenced in my CodeBehind:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Security.Principal;
    

    I am using the Web Forms Template as well. I do not know why I cannot access this method to display the users name. Do I have to configure my web application to use forms authentication? I thought this would be enough in my webconfig file:

     <authentication mode="Forms">
          <forms loginUrl="~/Account/Login" timeout="2880" defaultUrl="~/" />
        </authentication>

    Can anyone explain why I cannot access the Methods?

                       

    Saturday, April 19, 2014 2:42 PM

Answers

  • User753101303 posted

    None really.

    An aspx web page expose a Context property which in turn likely calls HttpContext.Current so using Context.User.Identity.Name or HttpContext.Current.User.Identity.Name is the same. BTW it makes me think that the Page class also expose directly the User property so finally you could even just use :
    User.Identity.Name and it will still work.

    The key point is just that there is no GetUserName() method but instead a Name property (you just have multiple ways to get at this property).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, April 19, 2014 5:51 PM

All replies

  • User753101303 posted

    Hi,

    Try Context.User.Identity.Name instead.

    Intellisense doesn't work here ? The error message is just true that is you don't have a GetUserName() method. Instead it is exposed as a Name property.

    Saturday, April 19, 2014 2:55 PM
  • User-1066344940 posted

    What is the difference between using:

    HttpContext.Current.User.Identity.Name

    And what you suggested? The above worked as well.

    Saturday, April 19, 2014 5:45 PM
  • User753101303 posted

    None really.

    An aspx web page expose a Context property which in turn likely calls HttpContext.Current so using Context.User.Identity.Name or HttpContext.Current.User.Identity.Name is the same. BTW it makes me think that the Page class also expose directly the User property so finally you could even just use :
    User.Identity.Name and it will still work.

    The key point is just that there is no GetUserName() method but instead a Name property (you just have multiple ways to get at this property).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, April 19, 2014 5:51 PM
  • User-1066344940 posted

    Thank you for the explanation. Cheers!

    Saturday, April 19, 2014 7:30 PM