简体   繁体   English

在Asp.net中创建登录并验证用户

[英]Create Login and Validating Users in Asp.net

I have created login page and i want to verify the users if they have access or not. 我已经创建了登录页面,我想验证用户是否具有访问权限。 I have done most of the work but just need couple of things here: 1) if user is validated, then i want to redirect to Home.aspx page 2) After the user is validated i want to be able to save the user id in a session so they don't have to login every time if they are redirected to another page. 我已经完成了大部分工作,但这里只需要几件事:1)如果用户通过了验证,那么我想重定向到Home.aspx页面2)用户通过验证后,我希望能够将用户ID保存在会话,因此如果将他们重定向到另一个页面,则不必每次都登录。 Basically, once they login successfully they should be able to navigate other pages in the application. 基本上,一旦他们成功登录,他们就应该能够浏览应用程序中的其他页面。 thanks. 谢谢。 here is the code behind: 这是背后的代码:

protected void ValidateUser3(object sender, EventArgs e)
        {
            int userId = 0;
            string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand("Validate_User"))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@Username", txtUID.Text.Trim());
                    cmd.Parameters.AddWithValue("@Password", txtPWD.Text.Trim());
                    cmd.Connection = con;
                    con.Open();
                    userId = Convert.ToInt32(cmd.ExecuteScalar());
                    con.Close();
                }
                switch (userId)
                {
                    case -1:
                        logLbl.Text = "Username and/or password is incorrect.";
                        break;
                    case -2:
                        logLbl.Text = "Account has not been activated.";
                        break;

                }
            }
        }

Here is my stored proc: 这是我存储的过程:

ALTER  PROCEDURE [dbo].[Validate_User]
    @Username NVARCHAR(20),
    @Password NVARCHAR(20)
AS
BEGIN
    SET NOCOUNT ON;
    DECLARE @UserId INT, @LastLoginDate DATETIME

    SELECT @UserId = UserId, @LastLoginDate = LastLoginDate 
    FROM myTable WHERE Username = @Username AND [Password] = @Password

    IF @UserId IS NOT NULL
    BEGIN
        IF NOT EXISTS(SELECT UserId FROM [Main].[UserActivation] WHERE UserId = @UserId)
        BEGIN
            UPDATE myTable.[dbo].[UserProfile]
            SET LastLoginDate =  GETDATE()
            WHERE UserId = @UserId
            SELECT @UserId [UserId] -- User Valid
        END
        ELSE
        BEGIN
            SELECT -2 -- User not activated.
        END
    END
    ELSE
    BEGIN
        SELECT -1 -- User invalid.
    END
END

First up I'd look at tightening that security. 首先,我将考虑加强这种安全性。 Don't save passwords in your database, not even hashed ones. 不要将密码保存在数据库中,甚至不要将密码保存在数据库中。 And follow krillgar's advice in the comments to your question :) 并在对您的问题的评论中遵循磷虾的建议:)

What you want to do with each user account is create a unique salt code - a random value, with a minimum of 8 in length (the more the better naturally): See here for creating a salt code . 您想要对每个用户帐户执行的操作是创建一个唯一的盐代码-一个随机值,长度至少为8(自然值越长越好): 请参见此处以创建盐代码 You want to save that in your database in the users table. 您要将其保存在用户表中的数据库中。 So you'll have something like a table structure like: UserId, UserName, PassString, SaltCode, Active 因此,您将拥有类似表结构的内容:UserId,UserName,PassString,SaltCode,Active

Pick a Hashing method; 选择一种哈希方法; MD5 or SHA1 are typical examples. MD5SHA1是典型示例。 When the password is created you take the username, merge it into the password, add your salt code then Hash it. 创建密码后,您将使用用户名,将其合并到密码中,添加盐码,然后将其哈希。 This will create a unique string - that's what you save into your database in the PassString field. 这将创建一个唯一的字符串-这就是您在PassString字段中保存到数据库中的内容。

So when someone logs in you take the username and password, look up the user record, get the salt code. 因此,当有人登录时,请使用用户名和密码,查找用户记录,获取盐码。 Hash it all together then compare the two hashed strings. 一起将它们哈希在一起,然后比较两个哈希字符串。

Check out this section on Stack Overflow: How to hash a password 查看有关堆栈溢出的这一部分: 如何对密码进行哈希处理

This makes your passwords irreversible without all segments of the data in other words; 也就是说,如果没有所有数据段,密码将无法恢复。 the password the user enters to login is the missing key to the puzzle. 用户输入的登录密码是拼图的缺失键。 If someone is somehow able to get the data out of your table they'll have to a ton of work to crack a user's account. 如果某人能够以某种方式从表中获取数据,那么他们将需要大量工作来破解用户的帐户。

so to now answer your actual question :) 因此,现在回答您的实际问题:)

Just like you have with your Stored Procedure, when the user is successfully matched return a value such as 1. I would suggest you keep your return codes and userid value separate so there's less ambiguity in your code. 就像存储过程一样,当用户成功匹配后,返回一个值,例如1。我建议您将返回代码和userid值分开,这样代码中的歧义就少了。 You got a choice with your SP, you can either return the userid back in with the return code on a Select statement ie Select 1, @UserId or you can use an OUTPUT parameter, here's a fairly decent example: Using stored procedure output parameters in C# (I'd favour the latter). 您可以选择SP,可以在Select语句(即Select 1, @UserId上使用返回代码返回用户ID Select 1, @UserId也可以使用OUTPUT参数,这是一个相当不错的示例:在中使用存储过程输出参数C# (我赞成后者)。

Now working with what you've got (and by that I mean, not telling you to go off an use System.IdentityModel.Service , UserPrinciple and the likes)... 现在使用您 UserPrinciple 东西 (这就是说,不告诉您停止使用System.IdentityModel.ServiceUserPrinciple等)...

Once that successful login has been acknowledged by your SP, use a session variable in the following manner, Session variables are kept server side so unless your server is compromised you'll be ok: 您的SP确认成功登录后,请按以下方式使用会话变量,会话变量将保留在服务器端,因此除非您的服务器受到威胁,否则您可以:

Session["UserId"] = userId;
Response.Redirect("Home.aspx");

Now on pages like Home.aspx you just need some code along the lines of; 现在在类似Home.aspx的页面上,您只需要一些代码即可;

bool userLoggedIn = false;
if (Session["User"] != null) //check the session variable is actually valid
{
    if (!String.IsNullOrEmpty(Session["User"]))
    {
        userLoggedIn = true;
    }
}

if (!userLoggedIn)
{
    Response.Redirect("login.aspx");
}

Though it would be a lot wiser to create a class in the App_Code folder to handle this, last thing you want is duplicated code all over your site. 尽管在App_Code文件夹中创建一个类来处理此问题要明智App_Code ,但您最后想要做的就是在整个站点中重复代码。

The issue you'll run into is giving user's rights. 您会遇到的问题是赋予用户权利。 I'm not clear from your question but I got the impression you wanted a binary login, ie: if a user is authenticated they have access, plain and simple. 我不清楚您的问题,但给您的印象是您想要一个二进制登录名,即:如果对用户进行身份验证,他们将具有简单,简单的访问权限。 If you're after segmented security; 如果您追求分段安全性; controlling what each user can or cannot do then you need to be looking down the route of Claims based security , here's an example of that to get you thinking: http://www.codeproject.com/Articles/2905/Role-based-Security-with-Forms-Authentication 控制每个用户可以做什么或不能做什么,那么您需要查看基于声明的安全性的路线,下面是一个使您思考的示例: http : //www.codeproject.com/Articles/2905/基于角色的-安全与-表单的身份验证

I hope that helps and wasn't too long a read :) 我希望这可以帮助并且不要太长时间阅读:)

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

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