如何通过在vb.net中进行编码来模拟 [英] how to do Impersonation by coding in vb.net

查看:71
本文介绍了如何通过在vb.net中进行编码来模拟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.


我的要求是,一旦用户登录到服务器Windows 2008 R2(IIS v7.5)上托管的应用程序,他就请求一些页面,然后又要切换为另一个用户,因为某些页面会调用taskcheduler.exe,这需要以管理员身份运行.
所以现在我想通过编码(VB.NET)进行模拟,除了声明到web.config文件中.

请帮助我
感谢您的帮助

Sanjay G.

Hi guys..


My requirement is that once a user is logged in an application which is hosted on server windows 2008 R2(IIS v7.5), he requests some pages and then he want to switch as another user because some page calls taskscheduler.exe which requires to run as an administrator.
so now i am trying to do impersonation by coding(VB.NET) except declare into the web.config file.

please help me
your help is appreciated

Sanjay G.

推荐答案

尝试一下
Public Class Form1
    Private Function IsAdmin() As Boolean
        Return My.User.IsInRole( _
             Microsoft.VisualBasic.ApplicationServices. _
             BuiltInRole.Administrator)
    End Function

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        If IsAdmin() = False Then
            If System.Environment.OSVersion.Version.Major = 6 Then
                MsgBox("Sorry... my app need to run as administrator" & vbCr & "Press ok to Restart in Administrator mode", MsgBoxStyle.Information)
                Dim process As System.Diagnostics.Process = Nothing
                Dim processStartInfo As System.Diagnostics.ProcessStartInfo

                processStartInfo = New System.Diagnostics.ProcessStartInfo()

                processStartInfo.FileName = "myapp.exe"

                If System.Environment.OSVersion.Version.Major >= 6 Then ' Windows Vista or higher
                    processStartInfo.Verb = "runas"
                Else
                    ' No need to prompt to run as admin
                End If

                processStartInfo.Arguments = ""
                processStartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal
                processStartInfo.UseShellExecute = True

                Try
                    process = System.Diagnostics.Process.Start(processStartInfo)
                Catch ex As Exception
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Finally

                    If Not (process Is Nothing) Then
                        process.Dispose()
                    End If

                End Try

            End If
            End
        End If


如果您的应用已启动由一个用户,代码正在运行该用户.普通用户不能模拟其他用户.只有setvice帐户或admin帐户可以模拟其他用户.

那么,您的代码是否在服务器上运行,并且在专门用于优化该应用程序的帐户下运行?
If your app is launched by a User, the code is running AS that User. Normal users cannot impersonate other users. Only a setvice account or admin account can impersonate other users.

So, is your code running on the server and under an account specifically created to tun this app?


这篇关于如何通过在vb.net中进行编码来模拟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆