Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从登录控件获取用户名并存储在数据库C中#_C#_Asp.net_Get - Fatal编程技术网

C# 从登录控件获取用户名并存储在数据库C中#

C# 从登录控件获取用户名并存储在数据库C中#,c#,asp.net,get,C#,Asp.net,Get,我是.NET的新手,目前正在建立一个网站,注册用户可以上传那里的文档,上传的文档可以由管理员发送。 我使用了一个登录控件来验证使用存储过程的用户。 现在我想当用户登录并上传文档时,他的名字也应该存储在数据库中(@Cname列),所以我试图从loginName控件获取label中的用户名 这是login.aspx页面代码 asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

我是.NET的新手,目前正在建立一个网站,注册用户可以上传那里的文档,上传的文档可以由管理员发送。 我使用了一个登录控件来验证使用存储过程的用户。 现在我想当用户登录并上传文档时,他的名字也应该存储在数据库中(@Cname列),所以我试图从loginName控件获取label中的用户名

这是login.aspx页面代码

asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
        <asp:Login ID="Login1" runat="server" OnAuthenticate="ValidateUser" BackColor="#F7F6F3" BorderColor="#E6E2D8" BorderPadding="4" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" Font-Size="0.8em" ForeColor="#333333" Height="256px" Width="536px">
        <InstructionTextStyle Font-Italic="True" ForeColor="Black" />
        <LoginButtonStyle BackColor="#FFFBFF" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" Font-Size="Large" ForeColor="#284775" Height="34px" Width="100px" />
        <TextBoxStyle Font-Size="X-Large" />
        <TitleTextStyle BackColor="#5D7B9D" Font-Bold="True" Font-Size="Large" ForeColor="White" />
    </asp:Login>    
</asp:Content>  
Login.aspx.cs代码为

protected void ValidateUser(object sender, EventArgs e)
        {
            int userId = 0;
            string roles = string.Empty;

            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", Login1.UserName);
                    cmd.Parameters.AddWithValue("@Password", Login1.Password);
                    cmd.Connection = con;
                    con.Open();
                    SqlDataReader reader = cmd.ExecuteReader();
                    reader.Read();
                    userId = Convert.ToInt32(reader["UserId"]);
                    roles = reader["Roles"].ToString();
                    con.Close();
                }
                switch (userId)
                {
                    case -1:
                        Login1.FailureText = "Username and/or password is incorrect.";
                        break;
                    case -2:
                        Login1.FailureText = "Account has not been activated.";
                        break;
                    default:
                        FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, Login1.UserName, DateTime.Now, DateTime.Now.AddMinutes(2880), Login1.RememberMeSet, roles, FormsAuthentication.FormsCookiePath);
                        string hash = FormsAuthentication.Encrypt(ticket);
                        HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash);

                        if (ticket.IsPersistent)
                        {
                            cookie.Expires = ticket.Expiration;
                        }
                        Response.Cookies.Add(cookie);
                        Response.Redirect(FormsAuthentication.GetRedirectUrl(Login1.UserName, Login1.RememberMeSet));
                        break;
                }
            }
        }
并验证用户存储过程

USE [LoginDB]
GO
/****** Object:  StoredProcedure [dbo].[Validate_User]    Script Date: 3/10/2016 10:37:39 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

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

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

    IF @UserId IS NOT NULL
    BEGIN
        IF NOT EXISTS(SELECT UserId FROM UserActivation WHERE UserId = @UserId)
        BEGIN
            UPDATE Users
            SET LastLoginDate =  GETDATE()
            WHERE UserId = @UserId

            SELECT @UserId [UserId], 
                    (SELECT RoleName FROM Roles 
                     WHERE RoleId = @RoleId) [Roles] -- User Valid
        END
        ELSE
        BEGIN
            SELECT -2 [UserId], '' [Roles]-- User not activated.
        END
    END
    ELSE
    BEGIN
        SELECT -1 [UserId], '' [Roles] -- User invalid.
    END
END
如何获取用户名??请在登录控件上提供帮助
Login
控件内的控件无法直接加入,但您需要使用该控件内的FindControl找到它们,例如:

TextBox txtUserName = (TextBox)Login1.FindControl("UserName");
如果在登录控件上看不到此行

<asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User Name:</asp:Label>
<asp:TextBox ID="UserName" runat="server" CssClass="textEntry"></asp:TextBox>
用户名:
然后您需要转换它并查看它的

在其他页面
要获取其他页面上的用户信息,请使用
Membership.GetUser()
,您可以使用
Membership.GetUser().UserName
作为用户名

对不起,我忘了告诉。。登录后,用户将被重定向到主页,在那里他必须上载文档。。ant您提供的解决方案无法在该页面上显示,因为登录控件位于登录页上page@VarunPanchal好的,然后使用
Membership.GetUser().UserName
我已经更新了我的答案不行了兄弟!!无法连接到SQL Server数据库。@VarunPanchal您还有其他错误。。。您的连接字符串错误。。。谷歌itno我的连接字符串工作正常,我可以登录注销。。每件事都在运行,用户角色等。。一切。我唯一的问题是我想在标签中显示用户名。。
<asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User Name:</asp:Label>
<asp:TextBox ID="UserName" runat="server" CssClass="textEntry"></asp:TextBox>