Вопрос

I am wanting to make data tables appear dynamically on a webpage based on the user logged in, and through various dropdown boxes and/or textbox inputs.

Before I start on scripting the dynamic functions, I want to make sure I get the user system working first. I have worked through many bugs with permissions to even get an editable database to show on a page, and for the login system.

After making the membership system finally work, I realized that there will be so little amount of users that I could manually create them in SQL Server Management Studio instead of using an ASP.net membership system. Although, I am not sure on the features/functions of each, and need advice before I get too much further.

What would be the best way to allow users to log in and access different databases using ASP.NET 4.0 and SQL server 2008?

Это было полезно?

Решение

Ok, so my end solution was to create users in SQL, and give them passwords, and assign databases there. Then when they log in, the program uses session variables to hold their login data. This code also tests the login info, and changes a label into an SQL error code, if one is produced. If the test passes, it simply redirects to the destination page.

Imports System.Data.SqlClient

Public Class login
Inherits System.Web.UI.Page


Protected Sub LoginButton_Click(sender As Object, e As EventArgs) Handles LoginButton.Click
    Session("Username") = textUserName.Text
    Session("Password") = textPassword.Text
    Session("Database") = textDatabase.Text
    Dim queryString As String = "LOGINPROPERTY ( '" + Session("Username") + "' , 'property_name' )"

    Session("ConnectionString") = "Data Source=MyServerNameHere;User ID=" + textUserName.Text + ";Password=" + textPassword.Text + ";database=" + textDatabase.Text

    Dim connection As New SqlConnection(Session("ConnectionString"))
    Dim command As New SqlCommand(queryString, connection)
    Try
        connection.Open() 
        Response.Redirect("destination.aspx")
    Catch ex As Exception
        LabelError.Text = ex.Message
    Finally
    End Try
End Sub
End Class

Each page which uses the database then has this code

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If (Session("ConnectionString") IsNot Nothing) Then
        SqlDataSource1.ConnectionString = Session("ConnectionString")
    End If
End Sub
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top