ASP.NET成员资格-向ASP.Net登录页面添加额外的字段 [英] ASP.NET Membership - Add extra field to ASP.Net Login page

查看:89
本文介绍了ASP.NET成员资格-向ASP.Net登录页面添加额外的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前在我的应用程序"中,使用ASP.Net成员资格提供程序验证用户,并且ASP.Net默认登录"页面仅包含用户名和密码.这在这两个领域都可以正常工作.但是根据我当前的要求,需要在ASP.Net登录页面中的下拉列表中显示一个额外的字段公司",并使用ASP.NET成员资格提供程序来验证这三个公司,用户名和密码.是否可能,请使用ASP.Net成员资格提供程序自定义带有额外字段的ASP.Net登录页面?您能给我一些想法,我该怎么办吗?

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?

推荐答案

成员资格提供程序的ValidateUser方法仅接受两个参数-用户名和密码.即使您覆盖它,也不能传递三个参数.

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
    }
}

请确保您使用新的 ASP.NET通用提供程序

Please make sure you use new ASP.NET Universal Providers

这篇关于ASP.NET成员资格-向ASP.Net登录页面添加额外的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆