如何显示登录名控制用户名 [英] how to display user name in login name control

查看:155
本文介绍了如何显示登录名控制用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有认为,在基于母版页上所有后续页面将出现一个LoginView内容的母版页。我有一个用户名控件也嵌套在一个LoginView显示,当他们在登录的用户的名称显示在code从母版页一个LoginView如下:

I have a master page that holds the loginview content that appears on all subsequent pages based on the master page. i have a username control also nested in the loginview to display the name of the user when they are logged in. the code for the loginview from the master page is displayed as follows:

<div class="loginView">
                <asp:LoginView ID="MasterLoginView" runat="server">
                    <LoggedInTemplate>
                        Welcome <span class="bold"><asp:LoginName ID="HeadLoginName" runat="server" /> 
                            <asp:Label ID="userNameLabel" runat="server" Text="Label"></asp:Label></span>!
                    [ <asp:LoginStatus ID="HeadLoginStatus" runat="server" LogoutAction="Redirect" LogoutText="Log Out" LogoutPageUrl="~/Logout.aspx"/> ]
                        <%--Welcome: 
                        <span class="bold"><asp:LoginName ID="MasterLoginName" runat="server" /> </span>!--%>                       
                    </LoggedInTemplate>
                    <AnonymousTemplate>
                        Welcome: Guest
                        [ <a href="~/Account/Login.aspx" ID="HeadLoginStatus" runat="server">Log In</a> ]
                    </AnonymousTemplate>

                </asp:LoginView>
                <%--&nbsp;&nbsp; [&nbsp;<asp:LoginStatus ID="MasterLoginStatus" runat="server" LogoutAction="Redirect" LogoutPageUrl="~/Logout.aspx" />&nbsp;]&nbsp;&nbsp;--%>

            </div>

由于VS2010与账户文件夹默认的登录页面启动,我不认为有必要创建一个单独的登录页面,所以我只是使用了相同的登录页面。请找code下面的登录控制:

Since VS2010 launches with a default login page in the accounts folder, i didnt think it necessary to create a separate log in page, so i just used the same log in page. please find the code for the login control below:

 <asp:Login ID="LoginUser" runat="server" EnableViewState="false" RenderOuterTable="false">
    <LayoutTemplate>
        <span class="failureNotification">
            <asp:Literal ID="FailureText" runat="server"></asp:Literal>
        </span>
        <asp:ValidationSummary ID="LoginUserValidationSummary" runat="server" CssClass="failureNotification" 
             ValidationGroup="LoginUserValidationGroup"/>
        <div class="accountInfo">
            <fieldset class="login">
                <legend style="text-align:left; font-size:1.2em; color:White;">Account Information</legend>
                <p style="text-align:left; font-size:1.2em; color:White;">
                    <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User ID:</asp:Label>
                    <asp:TextBox ID="UserName" runat="server" CssClass="textEntry"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName" 
                         CssClass="failureNotification" ErrorMessage="User ID is required." ToolTip="User ID field is required." 
                         ValidationGroup="LoginUserValidationGroup">*</asp:RequiredFieldValidator>
                </p>
                <p style="text-align:left; font-size:1.2em; color:White;">
                    <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label>
                    <asp:TextBox ID="Password" runat="server" CssClass="passwordEntry" 
                        TextMode="Password"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password" 
                         CssClass="failureNotification" ErrorMessage="Password is required." ToolTip="Password is required." 
                         ValidationGroup="LoginUserValidationGroup">*</asp:RequiredFieldValidator>
                </p>
                <p style="text-align:left; font-size:1.2em; color:White;">
                    <asp:CheckBox ID="RememberMe" runat="server"/>
                    <asp:Label ID="RememberMeLabel" runat="server" AssociatedControlID="RememberMe" CssClass="inline">Keep me logged in</asp:Label>
                </p>
            </fieldset>
            <p class="submitButton">
                <asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Log In" 
                    ValidationGroup="LoginUserValidationGroup" onclick="LoginButton_Click"/>
            </p>
        </div>
    </LayoutTemplate>
</asp:Login>

然后我写我自己的code认证,因为我有我自己的数据库。以下显示在登录按钮code。单击事件:

I then wrote my own code for authentication since i had my own database. the following displays the code in the login buttons click event.:

 public partial class Login : System.Web.UI.Page
{
    //create string objects
    string userIDStr, pwrdStr;

    protected void LoginButton_Click(object sender, EventArgs e)
    {


        //assign textbox items to string objects
        userIDStr = LoginUser.UserName.ToString();
        pwrdStr = LoginUser.Password.ToString();

        //SQL connection string

        string strConn;
        strConn = WebConfigurationManager.ConnectionStrings["CMSSQL3ConnectionString"].ConnectionString;

        SqlConnection Conn = new SqlConnection(strConn);



        //SqlDataSource CSMDataSource = new SqlDataSource();
       // CSMDataSource.ConnectionString = ConfigurationManager.ConnectionStrings["CMSSQL3ConnectionString"].ToString();


        //SQL select statement for comparison

        string sqlUserData;
        sqlUserData = "SELECT StaffID, StaffPassword, StaffFName, StaffLName, StaffType FROM Staffs";
        sqlUserData += " WHERE (StaffID ='" + userIDStr + "')";
        sqlUserData += " AND (StaffPassword ='" + pwrdStr + "')";

        SqlCommand com = new SqlCommand(sqlUserData, Conn);
        SqlDataReader rdr;
        string usrdesc;
        string lname;
        string fname;
        string staffname;

        try
        {

            //string CurrentData;
            //CurrentData = (string)com.ExecuteScalar();
            Conn.Open();
            rdr = com.ExecuteReader();
            rdr.Read();
            usrdesc = (string)rdr["StaffType"];
            fname = (string)rdr["StaffFName"];
            lname = (string)rdr["StaffLName"];
            staffname = lname.ToString() + " " + fname.ToString();
            LoginUser.UserName = staffname.ToString();
            rdr.Close();

            if (usrdesc.ToLower() == "administrator")
            {

                Response.Redirect("~/CaseAdmin.aspx", false);

             }
             else if (usrdesc.ToLower() == "manager")
             {
                 Response.Redirect("~/CaseManager.aspx", false);
             }
             else if (usrdesc.ToLower() == "investigator")
             {
                 Response.Redirect("~/Investigator.aspx", false);
             }
             else
             {
                 Response.Redirect("~/Default.aspx", false);
             }               


        }
        catch(Exception ex)
        {
            string script = "<script>alert('" + ex.Message + "');</script>";
        }
        finally
        {
            Conn.Close();
        }


    }

我的认证工作完全和页面被重定向到指定的目的地。然而,登录视图不显示用户名。其实,我无法弄清楚如何通过用户名,我已经从数据库中挑中显示的登录名控制。

My authentication works perfectly and the page gets redirected to the designated destination. However, the login view does not display the users name. i actually cant figure out how to pass the users name that i had picked from the database to the login name control to be displayed.

拍摄的密切关注我也注意到,应该显示成功日志中不显示后注销文本。这让我想知道,如果在母版中的loggedIn模板控制甚至火灾在其全部或仍是匿名的模板控件继续显示?

taking a close look i also noticed the logout text that should be displayed after successful log in does not show. that leaves me wondering if the loggedin template control on the masterpage even fires at all or its still the anonymous template control that keeps displaying.?

我如何得到这个工作如预期?请帮助....

How do i get this to work as expected? Please help....

推荐答案

我知道的是,登录控件绑定来从成员资格提供数据。
但是,由于您使用的是自己的登录系统。它无法获得登录控件来显示数据。

What i know is that Login controls are bound to Fetch data from Membership provider. But since you are using your own Login System. It can't get login controls to show data.

如果你想允许登录控件来显示数据自动然后使用类成员

If you want to allow Login Controls to show data automatically then Use Membership Classes

http://msdn.microsoft.com/en-us/library/ tw292whz.aspx

如果你想拥有自己的登录系统,然后,通过实施抽象类的MembershipProvider创建自定义成员提供程序。继承人在一个很好的资源

or if you want to have your own Login system Then Create a Custom membership provider by Implementing Abstract Class MembershipProvider. heres a good resource on that

HTTP:// WWW。 asp.net/general/videos/how-do-i-create-a-custom-membership-provider

另一种方法是手动更新上的控件的用户信息,如果你不希望使用成员提供。

Another way is to update user information Manually on the controls if you don't want to use membership provider..

这篇关于如何显示登录名控制用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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