ASP.NET Membership - Add extra field to ASP.Net Login page

人盡茶涼 提交于 2019-12-24 12:36:24

问题


Currently in My application, validating the users using the ASP.Net membership provider and the ASP.Net default Login page contains only Username and Password. This is working fine with this two fields. But as per my current requirement, need an extra field "Company" display as dropdownlist in ASP.Net Login page and validate the three Company, Username and Password using the ASP.NET membership provider. Is this possible or not, customize the ASP.Net Login page with an extra field using ASP.Net membership provider? Could you please give me some ideas how could I approach?


回答1:


Membership provider's ValidateUser method only accept two parameters - username and password. Even if you override it, you cannot pass three parameters.

ValidateUser(string username, string password)

Basically, you cannot use Login control to validate for user. Instead, you want to validate user by yourself. Here is an sample code -

Note: I strip out codes in Login control for demo purpose.

<asp:Login ID="LoginUser" runat="server" OnAuthenticate="LoginUser_Authenticate">
    <LayoutTemplate>
        <asp:TextBox ID="UserName" runat="server" />
        <asp:TextBox ID="Password" runat="server" TextMode="Password" />
        <asp:DropDownList ID="CompanyDropDownList" runat="server">
            <asp:ListItem Text="One" Value="1" />
            <asp:ListItem Text="Two" Value="2" />
        </asp:DropDownList>
        <asp:Button ID="LoginButton" runat="server" CommandName="Login" 
        Text="Log In" ValidationGroup="LoginUserValidationGroup" />
    </LayoutTemplate>
</asp:Login>

protected void LoginUser_Authenticate(object sender, AuthenticateEventArgs e)
{
    var companyName = LoginUser.FindControl("CompanyDropDownList") 
        as DropDownList;

    if(MyValidateUser(LoginUser.UserName, LoginUser.Password, 
        companyName.SelectedValue))
    {
        FormsAuthentication.SetAuthCookie(LoginUser.UserName, false);

        // Do something
    }
}

Please make sure you use new ASP.NET Universal Providers




回答2:


you can use the Profile Provider for this.

Alternatively, you could create your own table(s) that store additional user-related information. This is a little more work since you're on the hook for creating your own tables and creating the code to get and save data to and from these tables, but I find that using custom tables in this way allows for greater flexibility and more maintainability than the Profile Provider (specifically, the default Profile Provider, SqlProfileProvider, which stores profile data in an inefficient, denormalized manner).

Take a look at this tutorial, where you can find a walk through this process: Storing Additional User Information.



来源:https://stackoverflow.com/questions/15743637/asp-net-membership-add-extra-field-to-asp-net-login-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!