So, I'm trying to incorporate some kind of authentication into my WPF Application.
Currently, the application connects to a SQL Server, retrieves data from the database, inserts data into the database and updating data in the database. It works as intended.
What I would like to do next is when the Application starts up it checks whether the logged in user has permission to connect to and interact with the Server - and then deny access to the application or certain parts of it.
My SQL Server is using Windows Authentication for authentication, and the users will be on the same network and domain that will be connecting to the SQL Server.
I'm not very familiar with Windows Authentication and how it would work in my Application, so any help/examples/point in the right direction would greatly be appreciated.
[EDIT]: Thanks to Jan W who pointed me in the direction of checking the username against the server in the SQL layer with IS_MEMBER, I stumbled upon HAS_DBACCESS.
With a little bit of playing around, I created this function to check if the current user has access to the Database. If not, I disable what I need to in the application or give the user a message that they have not got permission:
Private Shared Function HasDBAccess(connectionString As String) As Boolean
Dim command As Data.SqlClient.SqlCommand = New Data.SqlClient.SqlCommand
Dim reader As Data.SqlClient.SqlDataReader
Dim conn As Data.SqlClient.SqlConnection = New Data.SqlClient.SqlConnection(connectionString)
command.CommandText = "SELECT HAS_DBACCESS ('yourdatabasenamehere')"
command.CommandType = Data.CommandType.Text
command.Connection = conn
conn.Open()
reader = command.ExecuteReader()
If reader.HasRows Then
Do While reader.Read()
Dim hasAccess As Integer = reader.GetInt32(0)
Select Case hasAccess
Case 1
Return True
Case 0
Return False
End Select
Loop
End If
conn.Close()
End Function
Thanks Jan :)