detailsview authenticate user before insert

I want to authenticate a user before allowing them to insert a new record.  I have a funtion to check the username and password are correct which returns true or false.

The details view has four fields, projectnumber, customer, startdate, and user.  user is the user id of the authenticated user.

My first question is where do I ask for the password, can I add a password field to the details view or do I need some other mechanism?

Secondly I know there is an ItemInserting for the details view or an inserting event for the sqldatasource, which is best to use and how do I allow the insert if authenticated or block it if not?


    Function AuthenticateUser(ByVal path As String, ByVal user As String, ByVal pass As String) As Boolean
        Dim dirEntry As New DirectoryEntry(path, user, pass)
        Try
            Dim nat As Object
            'if the NativeObject can be created using those credentials
            'then they are valid.
            nat = dirEntry.NativeObject
            Return True
        Catch
            Return False
        End Try
    End Function
iepaulAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ladarlingCommented:
My first question is where do I ask for the password, can I add a password field to the details view or do I need some other mechanism?
You could ask for the password in the detailsview.. And, In this scenario, I would create a class level boolean variable that indicated whether or not the user was authenticated.
Private UserIsAuthenticated As Boolean = False
And let your authentication function assign that value for the session. If you dont want that authentication to persist you would reset that value after the insert.
Secondly I know there is an ItemInserting for the details view or an inserting event for the sqldatasource, which is best to use and how do I allow the insert if authenticated or block it if not?
Basically, you just handle the ItemInserting event. The parameter object e will be a  DetailsViewInsertEventArgs
You can set its cancel property = True to abort the insert, based on whether the user is validated or not. Doing so in the page itself prevents you from having to rollback datasource changes.
0
iepaulAuthor Commented:
Do you have any samples of how this would work?  
0
ladarlingCommented:
It will look something like:
Sub CustomerDetail_ItemInserting(ByVal sender As Object, ByVal e As DetailsViewInsertEventArgs)
 'If the authentication flag is not set, cancel the insert method completely
If Not UserIsAuthenticated Then e.Cancel = True
End Sub

 
Here is more about the event itself:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsview.iteminserting.aspx
 
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.