简体   繁体   English

从其他页面访问登录控件

[英]Access Login Control from Different Page

I have a website in C# where users are authenticated to a SQL database via login control. 我在C#中有一个网站,通过登录控制用户可以通过SQL数据库进行身份验证。 Everything is working fine currently as I'm using the web.config to to direct to the Login.aspx page if the user isn't logged in. What I would like to do though is access the login controls from another page but additionally pass another parameter. 当前一切工作正常,因为如果用户未登录,我正在使用web.config定向到Login.aspx页面。我想做的是从另一个页面访问登录控件,但还要通过另一个参数。

So for example... 例如

The web.config has the following: web.config具有以下内容:

<authentication mode="Forms">
    <forms defaultUrl="~/Default.aspx" loginUrl="~/Login.aspx" slidingExpiration="true" timeout="20"></forms>
</authentication>
<authorization>
    <deny users="?"/>
</authorization>

The Login.aspx page looks like the following Login.aspx页面如下所示

protected void LoginControl_Authenticate(object sender, AuthenticateEventArgs e)
{
    bool authenticated = this.ValidateCredentials(LoginControl.UserName, LoginControl.Password);

    if (authenticated)
    {
        FormsAuthentication.RedirectFromLoginPage(LoginControl.UserName, LoginControl.RememberMeSet);
    }
}

private bool IsAlphaNumeric(string text)
{
    return Regex.IsMatch(text, "^[a-zA-Z0-9-]+$");
}

private bool ValidateCredentials(string userName, string password)
{
    bool returnValue = false;

    if (this.IsAlphaNumeric(userName) && userName.Length <= 25 && password.Length <= 50)
    {
        string sqlConn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

        using (SqlConnection sqlConnection1 = new SqlConnection(sqlConn))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = ("ValidateUser");
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("LoginName", userName.Trim());
                cmd.Parameters.AddWithValue("LoginPass", HashData.HashString(password.Trim()));
                cmd.Parameters.AddWithValue("Type", "Read");
                cmd.Connection = sqlConnection1;

                sqlConnection1.Open();

                if (cmd.ExecuteScalar() == null)
                {
                    returnValue = false;
                }

                else
                {
                    returnValue = true;
                }
            }
        }
    }

    return returnValue;
}

} }

Now what I would like to do is utilize the same login control across other pages so that I can see if the same user is logged in but pass a different "Type" parameter such as "Edit". 现在,我想做的是在其他页面上使用相同的登录控件,这样我就可以查看同一用户是否已登录,但可以传递不同的“ Type”参数,例如“ Edit”。

So the way I would like it to work is this... The users accesses the site and is redirected to the Login.aspx page. 因此,我希望它的工作方式如下:用户访问该站点,并重定向到Login.aspx页面。 The login control runs my stored procedure verifying they are "Read" type and redirects them to the Default.aspx. 登录控件将运行我的存储过程,以验证它们是否为“读取”类型,并将其重定向到Default.aspx。 From here a user can click an Edit button. 用户可以从此处单击“编辑”按钮。 Once they do, the same login control would check if they have "Edit" rights by running the same stored procedure but instead passing that as the "Type" parameter. 完成后,相同的登录控件将通过运行相同的存储过程,但将其作为“ Type”参数传递,来检查它们是否具有“编辑”权限。 At this point if the results are false the user would be prompted to login if their current rights don't allow it, or the page would just load if the current user has those rights. 此时,如果结果为假,则如果当前权限不允许该用户,则系统将提示用户登录,或者如果当前用户具有这些权限,则该页面仅会加载。 Is there a way to do what I"m looking for or would I need to just user either multiple login controls or different folder structure and do this all with web.config? 有没有一种方法可以满足我的需求,或者我只需要使用多个登录控件或不同的文件夹结构,并使用web.config来完成所有这些操作?

What you can do is create a Master page and put the Login Control in the Master page. 您可以做的是创建一个母版页,然后将登录控件放在母版页中。 Have each page that you want to be able to authenticate inherit from the Master page, which would give it access to the Login Control. 从母版页继承要认证的每个页,这将使它可以访问登录控件。

Problem solved... 问题解决了...

What I ended up doing was to let the login control authorize a user with the lowest level rights. 我最终要做的是让登录控件授权具有最低级别权限的用户。 When a user attempts to access a page that requires higher rights, I'm first checking if 当用户尝试访问需要更高权限的页面时,我首先要检查是否

 if (User.Identity.IsAuthenticated == true)

If true, then I run a new query that checks if User.Identity.Name is of the correct "Type". 如果为true,则运行一个新查询,以检查User.Identity.Name是否具有正确的“类型”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM