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>`

Login.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" />

login.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名が(マスターの)ユーザー名の最初の部分であると仮定して、コントロールを使用してログアウトテキストを設定できます。

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 に答える