1

我正在使用 masterPage.master 来获取我的应用程序的登录信息。当用户使用用户名/密码登录时,它会显示用户名已登录,我希望通过仅提取名字来使其更加个性化。我该怎么做,我尝试了所有我知道的,我是 ASP.Net 的新手。

母版页: `

<div id="login">
         <asp:LoginView ID="MasterLoginView" runat="server">
             <LoggedInTemplate>
                 Welcome:
                 <asp:LoginName ID="MasterLoginName" runat="server" Font-Bold="True" />
             </LoggedInTemplate>
             <AnonymousTemplate>
                 Welcome: Guest...
             </AnonymousTemplate>
         </asp:LoginView>
     </div>


<div id="login2">
  <asp:LoginStatus ID="LoginStatus1" runat="server" 
    LogoutAction="Redirect" LogoutPageUrl="access/LoggedOut.aspx" 
    LoginImageUrl="images/login.jpg" LogoutImageUrl="images/logout.jpg" LoginText="" 
    LogoutText="" />
</div>`

登录.aspx:

<asp:Label ID="Login1" runat="server"  Text="Label" ForeColor="White"></asp:Label>
<p>Username:
<asp:TextBox ID="txtusername" runat="server" Width="150px"></asp:TextBox></p>
<p>Password: 
<asp:TextBox ID="txtpassword" runat="server" Width="150px" TextMode="Password"></asp:TextBox> 
                                    </p>

<asp:ImageButton ID="LoginButton" runat="server" ImageUrl="../images/lsubmit.png"
OnClick="LoginBtnClick" ValidationGroup="a" ToolTip="Submit" ImageAlign="AbsBottom" />

登录.aspx.vb

Partial Public Class Login
    Inherits System.Web.UI.Page
    Private con As New SqlConnection()
    Private adp As SqlDataAdapter
    Private ds As New DataSet()

    Protected Sub LoginBtnClick(sender As Object, e As System.EventArgs) Handles LoginButton.Click
        Dim con As New SqlConnection("Data Source=HQDANA3A413VDB1;Initial Catalog=Sigar;User ID=Sigar_Admin;Password=$Jzpoj^6z>%y9E")
        con.Open()

        '   here with the help of this sql query i am matching the username and password of //the user.
        Dim cmd As New SqlCommand()
        cmd.CommandText = "select  * from Users where username=@username and password=@password"

        cmd.Connection = con
        '   here i am passing the parameter of username and  password
        cmd.Parameters.Add("@username", SqlDbType.VarChar, 50).Value = txtusername.Text
        cmd.Parameters.Add("@password", SqlDbType.VarChar, 50).Value = txtpassword.Text

        Dim firstName As New SqlParameter("@firstname", SqlDbType.VarChar, 50, ToString)
        firstName.Value = ToString()
        cmd.Parameters.Add(firstName)

        Dim dr As SqlDataReader = cmd.ExecuteReader()
        '   here i am using hasrows to check the correct user. If the username and the password 
        '//of the user will be mathed with the database then it will be go to the checkuser.aspx //page otherwise it prints the message wrongusername or password     
        If dr.HasRows Then
            dr.Read()
            '   here i am creating a formauthentication ticket that will be use in  the whole //application. 
            'This is the main part of the formauthentication security, inside dr[2] //there is  a role of the user
            Dim tkt As New FormsAuthenticationTicket(1, txtusername.Text.ToUpper, DateTime.Now, DateTime.Now.AddHours(3), False, dr(2).ToString(), _
             FormsAuthentication.FormsCookiePath)
            '   here i am enctypt the ticket. With the encryption  of this ticket it will encrypt the //username
            Dim st As [String] = FormsAuthentication.Encrypt(tkt)
            '   here i am creat a cookie that will we used inside the whole application
            Dim ck As New HttpCookie(FormsAuthentication.FormsCookieName, st)
            Response.Cookies.Add(ck)

            ' Validate the user against the Membership framework user store
            'If Membership.ValidateUser(txtusername.Text, txtpassword.Text) Then
            ' Log the user into the site
            'FormsAuthentication.RedirectFromLoginPage(txtusername.Text, RememberMe.Checked)

            Response.Redirect("checkuser.aspx")
        Else
            LabelFailedAuthentication.Visible = True
        End If


    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        CancelButton.Attributes.Add("onClick", "javascript:history.back(); return false;")
    End Sub

End Class
4

1 回答 1

0

您可以使用LoginStatus控件设置注销文本,假设名字是您的用户名的第一部分(在 Master 中):

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Me.LoginStatus1.LogoutText = "Logout: " & GetUserName()
End Sub

Public Function GetUserName() As String
    If HttpContext.Current.User.Identity.IsAuthenticated Then
        If ViewState("UserName") Is Nothing Then
            Dim user As MembershipUser = Membership.GetUser()
            ' split user-name, assuming that the first part is the firstname '
            Dim nameParts = user.ToString().Split()
            ViewState("UserName") = nameParts(0)
        End If
        Return DirectCast(ViewState("UserName"), String)
    Else
        Return String.Empty
    End If
End Function
于 2012-10-30T16:12:11.633 回答