ChrisOPeterson
ChrisOPeterson

Reputation: 559

Asp.Net authentication with email address

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.

Upvotes: 1

Views: 1891

Answers (4)

user3386384
user3386384

Reputation: 1

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.

Upvotes: 0

rgmills
rgmills

Reputation: 393

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.

Upvotes: 2

smartcaveman
smartcaveman

Reputation: 42276

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

Upvotes: 1

jimmystormig
jimmystormig

Reputation: 10802

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;

Upvotes: 1

Related Questions