我正在开发一个ASP.net应用程序,我正在尝试冒充用户
我正在使用令牌创建一个windowsIdentity
WindowsIdentity winId = new WindowsIdenty( token );
Run Code Online (Sandbox Code Playgroud)
这个令牌是通过调用un托管代码获得的
[DllImport("advapi32.dll")]
public static extern int LogonUserA(String lpszUserName,
String lpszDomain,
String lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken);
Run Code Online (Sandbox Code Playgroud)
有没有其他方法来获取令牌而不使用此advapi32.dll非托管代码?
TKS
我正在使用以下代码通过Kerberos进行身份验证.
IntPtr logonToken = WindowsIdentity.GetCurrent().Token;
string authenticationType = "WindowsAuthentication";
WindowsIdentity windowsIdentity = new WindowsIdentity(logonToken, authenticationType);
//windowsIdentity.Name == equals "IIS APPPOOL\Classic .NET AppPool" when I want it to be the user
Run Code Online (Sandbox Code Playgroud)
这只有在我尝试运行我的.NET应用程序Web服务器时才会发生.如果我在我的机器上本地运行代码进行调试,它会在Name属性中显示我的用户ID.有关如何在Web服务器上运行的任何建议?
我是一名开发人员,我已经找到了一个Web服务身份验证问题的解决方案,该问题涉及确保由于多个网络跃点而维护Kerberos.简而言之:
是否有一种脚本语言可用于为此应用程序设置新的应用程序池,然后按照描述设置标识(而不是手动数据输入到IIS中的属性页)?
我认为我们的系统管理员对Powershell有一点了解,但有人可以帮助我为他提供一些东西(他需要在推出应用程序时在另外两台服务器上重复这一点).谢谢.
我正在尝试在WIF中设置滑动会话,并且需要处理SessionSecurityTokenReceived.
我确定我在这里做了一些愚蠢的事......但是VS2010继续告诉我There is no applicable variable or member在下面说明的地方.谁能指出我正确的方向?我已经搜索了如何定义这个事件的处理的实际样本的高低,但我找不到一个.
Global.asax中
protected void Application_Start()
{
FederatedAuthentication.WSFederationAuthenticationModule.SecurityTokenReceived
+= SessionAuthenticationModule_SessionSecurityTokenReceived;
// ^^^ There is no applicable variable or member
}
void SessionAuthenticationModule_SessionSecurityTokenReceived(object sender, SessionSecurityTokenReceivedEventArgs e)
{
DateTime now = DateTime.UtcNow;
DateTime validFrom = e.SessionToken.ValidFrom;
DateTime validTo = e.SessionToken.ValidTo;
if ((now < validTo) &&
(now > validFrom.AddMinutes((validTo.Minute - validFrom.Minute) / 2))
)
{
SessionAuthenticationModule sam = sender as SessionAuthenticationModule;
e.SessionToken = sam.CreateSessionSecurityToken(
e.SessionToken.ClaimsPrincipal,
e.SessionToken.Context,
now,
now.AddMinutes(2),
e.SessionToken.IsPersistent);
e.ReissueCookie = true;
} …Run Code Online (Sandbox Code Playgroud) 可能重复:
如何判断我的进程是否以管理员身份运行?
如果我的进程作为本地管理员运行,我如何使用C#进行检查?
我知道如何确定当前用户是否是内置管理员组的成员.但这不是我想知道的.我想知道当前用户是否是(唯一的)特殊本地管理员帐户.
我也知道如何检索当前用户的名称,但我不想将其与硬编码名称"Administrator"进行比较,因为这不适用于Windows的本地化版本(例如西班牙语中的"Administrador","Administrateur")用法语等).
我启用了模拟和Windows身份验证.
<authentication mode="Windows" />
<identity impersonate="true" userName="name" password="passord"/>
Run Code Online (Sandbox Code Playgroud)
但返回经过身份Thread.CurrentPrincipal.Identity.Name验证的用户的名称并WindowsIdentity.GetCurrent()返回模拟身份.
这些身份不应该相同吗?
在这种情况下,代码运行的凭据是什么?
asp.net security impersonation windows-identity current-principal
我在访问数据库时想要一个服务,我想访问数据库以使用调用身份凭证.
在我访问特定数据库之前,我会进行模拟
var winId = HttpContext.Current.User.Identity as WindowsIdentity;
var ctx = winId.Impersonate();
//Access Database
ctx.Undo();
Run Code Online (Sandbox Code Playgroud)
当服务在我的PC上本地运行时,此方案可以正常工作.但是,当部署在另一台远程PC上时,我收到错误:
用户"NT Authority\Anonymous Logon"登录失败
一旦它尝试访问数据库.
DBAdmin告诉我,SQL Server有一个SPN.
运行服务的帐户是域帐户.
用户触发事件,因此线程在所述用户的上下文中.我遍历所有连接的用户并希望向他们发送此事件,但我想使用经典授权来确定他们是否应该接收该事件.问题是线程主体等是在触发事件的用户的上下文中.
我正在考虑模仿线程,然后进行授权.我发现了这篇文章
http://msdn.microsoft.com/en-us/library/ff647248.aspx#ImpersonationDelegation5
using System.Security.Principal;
…
WindowsIdentity wi = new WindowsIdentity(userName@fullyqualifieddomainName);
WindowsImpersonationContext ctx = null;
try
{
ctx = wi.Impersonate();
// Thread is now impersonating you can call the backend operations here...
catch
{
// Prevent exceptions propagating.
}
finally
{
// Ensure impersonation is reverted
ctx.Undo();
}
Run Code Online (Sandbox Code Playgroud)
之后ctx = wi.Impersonate();的线程仍然在主叫用户的情况下,我究竟做错了什么?
更新 我想以其他用户身份运行此代码
provider = AuthorizationFactory.GetAuthorizationProvider();
provider.Authorize(Thread.CurrentPrincipal, Rule)
Run Code Online (Sandbox Code Playgroud)
我做的一个小博客,涵盖了所需的步骤 http://andersmalmgren.com/2014/06/12/client-server-event-aggregation-with-role-authorization/
我们如何WindowsIdentity在 .NET Core 2.1 中使用?
它在 2.0 中可用
.NET API 浏览器
我正在尝试将现有文件从特定文件夹复制到共享文件夹.所以这是代码:
if (!System.IO.File.Exists(fullPath))
{
using (WindowsIdentity.GetCurrent().Impersonate())
{
try
{
image.Save(fullPath);
System.Security.AccessControl.DirectorySecurity sec = System.IO.Directory.GetAccessControl(originalDocumentFolderPath);
FileSystemAccessRule accRule = new FileSystemAccessRule(originalDocumentFolderPath, FileSystemRights.FullControl, AccessControlType.Allow);
sec.AddAccessRule(accRule);
string sharedFolderPath = "\\" + Path.Combine(Environment.MachineName, "Users");
sharedFolderPath = Path.Combine(sharedFolderPath, username);
sharedFolderPath = Path.Combine(sharedFolderPath, "Desktop");
sharedFolderPath = Path.Combine(sharedFolderPath, "SharedFolder");
System.IO.File.Copy(originalDocumentFolderPath, sharedFolderPath);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我收到这个错误:
System.Security.Principal.IdentityNotMappedException:'无法翻译部分或全部身份引用.'
在这一行:
sec.AddAccessRule(accRule);
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?如果您需要更多数据,请告诉我们......
编辑:
此外,最终目标是这实际上应该将文件保存到LAN网络中特定计算机上的共享文件夹中,但我目前正在尝试将其保存在程序运行的同一台计算机上的共享文件夹中.
编辑2:
所以我尝试了@PaulKaram的建议,但我仍然得到下一个错误:
从图片中,可以看到文档中我首先保存图像的文件夹.这没有问题.当我尝试将其复制到桌面上的特定共享文件夹时,出现在文档中已创建的文件夹上面的错误(访问被拒绝).
windows-identity ×10
c# ×7
asp.net ×2
.net ×1
advapi32 ×1
events ×1
global-asax ×1
iis ×1
kerberos ×1
powershell ×1
security ×1
sql-server ×1
wif ×1
winforms ×1