1

I am using an email address as my "username" for Login. When a user logs in the User object that is created has the Email address as the name which is incorrect. The actual name is another field in the DB. How do I login and authenticate against the email address and then load the correct name in the User object?

I am using forms authentication with a custom MembershipProvider and RoleProvider.

Edits Below to Clarify

I have a custom MembershipProvider that validates users by email address and password. It is called by the basic Login Control and registered in web.config.

<asp:Login ID="AdminLogin" runat="server" MembershipProvider="AdminMembershipProvider">
</asp:Login>

Web.Config:

<authentication mode="Forms">
        <forms loginUrl="Default.aspx" />
    </authentication>
    <membership defaultProvider="AdminMembershipProvider">
      <providers>
        <clear />
        <add name="AdminMembershipProvider"
             type="AdminMembershipProvider"
             connectionStringName="EvalSysConnectionString"
             />

      </providers>
    </membership>

With just this my users are logged in correctly. However it returns a Context.User.Identity.Name of the email address. I have the first name and last name stored in the DB and want this to be the Name.

I read various posts that seem to indicate that I would have to create a Global.asax file and override the PostAutheticateRequest method to do this. However I have no idea how to proceed with this strategy.

<%@ Application Language="C#" %>

<script runat="server">

    protected void Application_PostAuthenticateRequest()
    {
        string test = HttpContext.Current.User.ToString();
    }

</script>

I'm sorry my question was so vague. I'm so lost with this that I don't know how to write a good question.

2
  • question makes little sense .... What have you tried? Commented Mar 6, 2011 at 3:00
  • Agree with Mitch. You need to post what you've tried, how your MembershipProvider is called, etc. Commented Mar 6, 2011 at 3:08

4 Answers 4

2

I believe what you are asking is: Can I change the property Context.User.Identity.Name to display something other than the username?

EDIT

It is not possible using the implementation of the property Context.User.Identity.Name as it is read-only and is set to the username with no way to change it.

A quick and easy solution

You can write a method that calls a stored procedure (or query in your code) in your database to do a lookup based on the value in Context.User.Identity.Name and returns First and Last. You can store the result in a Session variable or use the Profile object (as noted below).

Alternate solution

You can write your own implementation of IIdentity. It's something I've never done, so I don't have any snippets to contribute. However this link should give you a place to start. I'd be careful with this solution since you lose your unique way of identifying a user unless you add an additional property to your implementation of IIdentity and make sure you cast Context.User.Identity, whenever referenced, to your own implementation.

Hope that helps.

Sign up to request clarification or add additional context in comments.

4 Comments

Thats not exactly what i'm asking. The username and password authentication occurs against the email address and password. I then have the first name and last name of the user stored that I want to be the Name stored in Context.User.Identity.Name. If that makes sense....
It does, however Context.User.Identity.Name is read-only. I'd suggest pulling the information as needed by doing a database lookup and store in a session variable or Profile object as mentioned below.
I'm working on that right now and it seems to be the best direction. Why are there so many disparate parts to the asp.net login scheme? Its confusing the hell out of me!
Been a learning experience for me too, PITA, hah. Good luck with it. Just seems cleaner this way, otherwise you can imagine it could be more of a pain if you had users with the same First and Last name. When you wanted to do an update, you'd have no reference to which actual user you are wanting to update.
1

I would do exactly what you have done so far and then store the users name, address and so on in the Profile object.

This is as easy as adding some configuration (web.config):

<profile>
  <providers>
    <clear />
    <add name="SqlProvider"
      type="System.Web.Profile.SqlProfileProvider"
      connectionStringName="SqlServices"
      applicationName="SampleApplication"
      description="SqlProfileProvider for SampleApplication" />
  </providers>
  <properties>
    <add name="Name" />
  </properties>
</profile>

And then retrieve/set it like so:

txtName.Text = Profile.Name;
Profile.Name = txtName.Text;

1 Comment

This was the way I decided to go. For my purposes it was the easiest to implement though it took more work than expected. Thanks Jimmy!
1

You could definitely look into using a Profile provider like Jimmy suggested. Or, you could implement an IPrincipal and set the HttpContext.User property in your PostAuthenticateRequest event.

Basically, you just do something like

        var userName = Database.GetUserName(httpContext);
        httpContext.User = new GenericPrincipal(new GenericIdentity(userName, "FORMS"), new[] {"UserRole1"});

The first line is an example of a call to your db to get the username. The second line creates a principal and identity from this info then assigns it to the User property of the HttpContext

Comments

0
Dim username As String = Membership.GetUserNameByEmail(txtUsername.Text)
If (Membership.ValidateUser(username, txtPassword.Text)) Then

Here is some Vb code. that seems to retain the columns in the Membership Provider. Basically you allow a user to enter their email address which is unique and get their username using an email address. Then use the validation method to authenticate the user by their username and password.

What do you think guys.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.