I have a fairly basic VB.NET project that allows users to restore certain parts of their profiles, should it ever need to be recreated (IE Favourites, Quick Launch, that sort of thing).

I have added to the project the ability to restore these profile parts on behalf of other users, but for this you should have Administrator privileges for our Domain, and I wish the project to prompt for appropriate credentials.

Is this possible for only part of a project? I've looked in to manifests, but from my (limited) understanding it seems that they are only appropriate for for projects as a whole, as opposed to components of a project. Thanks.

If Username = "" Then
    Return False
ElseIf Not Username = CurrentUsername Then

    '** Require admin privilages *'

Else
    Return True
End If
有帮助吗?

解决方案 2

I figured this out in the end (almost). The code below uses a Username and Password supplied in an optional 'Settings' form to check whether the current user is a member of the built-in Administrators group in our Domain.

Public Function IsAuthenticatedUser(ByVal Username As String,
                                    Optional ByVal Group As String = "Administrators") As Boolean

    Dim IsAuthenticated As Boolean = False

    Try
        Using RootContext2 As New PrincipalContext(ContextType.Domain,
                                                   "dynedrewett.com",
                                                   "DC=dynedrewett,DC=com",
                                                   Me.formSettings.txtUsername.Text,
                                                   Me.formSettings.txtPassword.Text & "XXX"), _
            TheGroup As GroupPrincipal = GroupPrincipal.FindByIdentity(RootContext2, IdentityType.Name, Group), _
            TheUser As UserPrincipal = UserPrincipal.FindByIdentity(RootContext2, IdentityType.SamAccountName, Username)

            If TheGroup IsNot Nothing AndAlso TheUser IsNot Nothing Then

                For Each SingleGroup As Principal In TheGroup.GetMembers(True)
                    If SingleGroup.Name = TheUser.DisplayName Then
                        IsAuthenticated = True
                        Exit For
                    End If
                Next

            Else
                IsMember = False
            End If

            TheGroup.Dispose()
            TheUser.Dispose()

        End Using
    Catch Ex As Exception
        Dim ErrorForm As New formError(Ex, "Ensure that valid Administrator Credentials are specified in the application Settings.")
    End Try

    Return IsAuthenticated

End Function

其他提示

Answering your comment i got this:

If My.User.IsInRole(ApplicationServices.BuiltInRole.Administrator) Then
        MessageBox.Show("Yes")
    Else
        MessageBox.Show("No")
    End If

It tells if your application is being runned as admin or not.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top