关于共享文件夹

thelostman 2003-03-10 09:53:44
关于共享文件夹,只给b用户操作的权力,能否用a的用户名在windows登陆,在不注销a的身份的情况下,在程序里以的b身份进行文件写读操作?---目的就是一个:保护特殊的共享目录。
...全文
45 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
thelostman 2003-03-12
  • 打赏
  • 举报
回复
那就强烈要求装2000 xp不久完了。
sumtec 2003-03-12
  • 打赏
  • 举报
回复
大家注意哦!注意什么呢?仔细看MSDN!
MSDN上面说,此方法不适用于Win98/WinMe!
thelostman 2003-03-12
  • 打赏
  • 举报
回复
是啊,以前不知道还可以这样,谢谢老大们,有了希望至少成功了一半。
sumtec 2003-03-11
  • 打赏
  • 举报
回复
hehe, 原来dragnott发的是MSDN上面的东西:

ms-help://MS.VSCC/MS.MSDNVS.2052/cpref/html/frlrfsystemsecurityprincipalwindowsidentityclassimpersonatetopic1.htm

Imports System
Imports System.Runtime.InteropServices
Imports System.Security.Principal
Imports System.Security.Permissions

<Assembly:SecurityPermissionAttribute(SecurityAction.RequestMinimum, UnmanagedCode := true)>
Public Class Impersonation

<DllImport("C:\\WINNT\\System32\\advapi32.dll")> _
Public Shared Function LogonUser(lpszUsername As String, lpszDomain As String, lpszPassword As String, _
dwLogonType As Integer, dwLogonProvider As Integer, ByRef phToken As Integer) As Boolean
End Function

<DllImport("C:\\WINNT\\System32\\Kernel32.dll")> _
Public Shared Function GetLastError() As Integer
End Function

Public Shared Sub Main(args() As String)

'The Windows NT user token.
Dim token1 As Integer

'Get the user token for the specified user, machine, and password using the unmanaged LogonUser method.

'The parameters for LogonUser are the user name, computer name, password,
'Logon type (LOGON32_LOGON_NETWORK_CLEARTEXT), Logon provider (LOGON32_PROVIDER_DEFAULT),
'and user token.
Dim loggedOn As Boolean = LogonUser("bob", "AARDVARK", "coffee", 3, 0, token1)
Console.WriteLine("LogonUser called")

'Call GetLastError to try to determine why logon failed if it did not succeed.
Dim ret As Integer = GetLastError()

Console.WriteLine("LogonUser Success? " + loggedOn)
Console.WriteLine("NT Token Value: " + token1)
If ret <> 0 Then
Console.WriteLine("Error code (126 == ""Specified module could not be found""): " + ret)
End If

'Starting impersonation here:
Console.WriteLine("Before impersonation:")
Dim mWI1 As WindowsIdentity = WindowsIdentity.GetCurrent()
Console.WriteLine(mWI1.Name)
Console.WriteLine(mWI1.Token)

Dim token2 As IntPtr = new IntPtr(token1)

Console.WriteLine("New identity created:")
Dim mWI2 As WindowsIdentity = new WindowsIdentity(token2)
Console.WriteLine(mWI2.Name)
Console.WriteLine(mWI2.Token)

'Impersonate the user.
Dim mWIC As WindowsImpersonationContext = mWI2.Impersonate()

Console.WriteLine("After impersonation:")
Dim mWI3 As WindowsIdentity = WindowsIdentity.GetCurrent()
Console.WriteLine(mWI3.Name)
Console.WriteLine(mWI3.Token)

'Revert to previous identity.
mWIC.Undo()

Console.WriteLine("After impersonation is reverted:")
Dim mWI4 As WindowsIdentity = WindowsIdentity.GetCurrent()
Console.WriteLine(mWI4.Name)
Console.WriteLine(mWI4.Token)
End Sub
End Class
sumtec 2003-03-11
  • 打赏
  • 举报
回复
hehe, 楼上的真有趣,这里可是VB.NET版……

不过我倒是学到了一样东西:
SecurityPermissionAttribute(SecurityAction.RequestMinimum, UnmanagedCode=true)
就是不知道能不能在VB.NET里面使用,改天我试试。

楼主:不要着急,会有人来给你翻译的。
dragontt 2003-03-11
  • 打赏
  • 举报
回复
这样写好象程序不好控制
给你换一个方法把
用WindowsImpersonationContext 类

using System;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Security.Permissions;

[assembly:SecurityPermissionAttribute(SecurityAction.RequestMinimum, UnmanagedCode=true)]
public class Class1
{
[DllImport("C:\\WINNT\\System32\\advapi32.dll")]
public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,
int dwLogonType, int dwLogonProvider, out int phToken);

[DllImport("C:\\WINNT\\System32\\Kernel32.dll")]
public static extern int GetLastError();

public static void Main(string[] args)
{
// The Windows NT user token.
int token1;

// Get the user token for the specified user, machine, and password using the unmanaged LogonUser method.

bool loggedOn = LogonUser(
// User name.
"bob",

// Computer name.
"AARDVARK",

// Password.
"coffee",

// Logon type = LOGON32_LOGON_NETWORK_CLEARTEXT.
3,

// Logon provider = LOGON32_PROVIDER_DEFAULT.
0,

// The user token for the specified user is returned here.
out token1);

Console.WriteLine("LogonUser called");

// Call GetLastError to try to determine why logon failed if it did not succeed.
int ret = GetLastError();

Console.WriteLine("LogonUser Success? " + loggedOn);
Console.WriteLine("NT Token Value: " + token1);
if (ret != 0) Console.WriteLine("Error code (126 == \"Specified module could not be found\"): " + ret);

//Starting impersonation here:
Console.WriteLine("\n\nBefore impersonation:\n");
WindowsIdentity mWI1 = WindowsIdentity.GetCurrent();
Console.WriteLine(mWI1.Name);
Console.WriteLine(mWI1.Token);

IntPtr token2 = new IntPtr(token1);

Console.WriteLine("\n\nNew identity created:\n");
WindowsIdentity mWI2 = new WindowsIdentity(token2);
Console.WriteLine(mWI2.Name);
Console.WriteLine(mWI2.Token);

// Impersonate the user.
WindowsImpersonationContext mWIC = mWI2.Impersonate();

Console.WriteLine("\n\nAfter impersonation:\n");
WindowsIdentity mWI3 = WindowsIdentity.GetCurrent();
Console.WriteLine(mWI3.Name);
Console.WriteLine(mWI3.Token);

// Revert to previous identity.
mWIC.Undo();

Console.WriteLine("\n\nAfter impersonation is reverted:\n");
WindowsIdentity mWI4 = WindowsIdentity.GetCurrent();
Console.WriteLine(mWI4.Name);
Console.WriteLine(mWI4.Token);
}
}
dragontt 2003-03-11
  • 打赏
  • 举报
回复
定义b用户身份
WindowsIdentity MyIdentity;
//Put the previous identity into a principal object.
WindowsPrincipal MyPrincipal = new WindowsPrincipal(MyIdentity);
设定当前线程的权限
Thread.CurrentPrincipal = MyPrincipal;
Montaque 2003-03-11
  • 打赏
  • 举报
回复
肯定可以。

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧