- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个允许使用表单登录和 Windows 身份验证登录的 MVC 站点。我使用自定义 MembershipProvider 对用户进行 Active Directory 身份验证,使用 System.Web.Helpers AntiForgery 类进行 CSRF 保护,以及 Owin cookie 身份验证中间件。
在登录期间,一旦用户通过 Active Directory 身份验证,我将执行以下操作:
IAuthenticationManager authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
authenticationManager.SignOut(StringConstants.ApplicationCookie);
var identity = new ClaimsIdentity(StringConstants.ApplicationCookie,
ClaimsIdentity.DefaultNameClaimType,
ClaimsIdentity.DefaultRoleClaimType);
if(HttpContext.Current.User.Identity is WindowsIdentity)
{
identity.AddClaims(((WindowsIdentity)HttpContext.Current.User.Identity).Claims);
}
else
{
identity.AddClaim(new Claim(ClaimTypes.Name, userData.Name));
}
identity.AddClaim(new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider", "Active Directory"));
identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, userData.userGuid));
authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, identity);
IAuthenticationManager authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
authenticationManager.SignOut(StringConstants.ApplicationCookie);
Window.location
更新到网站的主页。
IntegratedWindowsAuthentication
登录(IWA) 有效,但我在使用 IWA 登录时遇到了问题。这是发生的事情:
User.Identity
此时的类型为 WindowsIdentity
与 AuthenticationType
设置为 Negotiate
,而不是我所期望的,ClaimsIdentity
创建于 SignIn
方法同上。@AntiForgery.GetHtml()
为用户准备主页在 View 中。这样做是为了使用登录用户的详细信息创建新的 AntiForgery token 。 token 是用 WindowsIdentity
创建的ClaimsIdentity
到达。 !第一POST
因此,请求到达不可避免地会导致 AntiForgeryException
它发送的防伪 token 是“针对不同用户的”。 ClaimsIdentity
并允许
POST
要求发挥作用。
POST
请求可能会到达
WindowsIdentity
而不是与
ClaimsIdentity
,再次抛出
AntiForgeryException
.
User.Identity
上遗漏了一些东西或者我在登录过程中做错了什么......有什么想法吗?
AntiForgeryConfig.SuppressIdentityHeuristicChecks = true;
允许
AntiForgery.Validate
Action 是否成功
WindowsIdentity
或
ClaimsIdentity
已收到,但正如 MSDN 上所述:
Use caution when setting this value. Using it improperly can open security vulnerabilities in the application.
最佳答案
原来问题是 ClaimsPrincipal 支持多个身份。如果您处于拥有多个身份的情况下,它会自行选择一个。我不知道是什么决定了 IEnumerable 中身份的顺序,但无论它是什么,它显然肯定会在用户 session 的生命周期中产生一个恒定的顺序。
正如 asp.net/Security git 的问题部分所述,NTLM and cookie authentication #1467 :
Identities contains both, the windows identity and the cookie identity.
It looks like with
ClaimsPrincipals
you can set astatic Func<IEnumerable<ClaimsIdentity>, ClaimsIdentity>
calledPrimaryIdentitySelector
which you can use in order to select the primary identity to work with.
static ClaimsIdentity MyPrimaryIdentitySelectorFunc(IEnumerable<ClaimsIdentity> identities)
ClaimsIdentity
的列表s 并选择您喜欢的一种。
PrimaryIdentitySelector
,像这样:
System.Security.Claims.ClaimsPrincipal.PrimaryIdentitySelector = MyPrimaryIdentitySelectorFunc;
PrimaryIdentitySelector
方法最终看起来像这样:
public static ClaimsIdentity PrimaryIdentitySelector(IEnumerable<ClaimsIdentity> identities)
{
//check for null (the default PIS also does this)
if (identities == null) throw new ArgumentNullException(nameof(identities));
//if there is only one, there is no need to check further
if (identities.Count() == 1) return identities.First();
//Prefer my cookie identity. I can recognize it by the IdentityProvider
//claim. This doesn't need to be a unique value, simply one that I know
//belongs to the cookie identity I created. AntiForgery will use this
//identity in the anti-CSRF check.
var primaryIdentity = identities.FirstOrDefault(identity => {
return identity.Claims.FirstOrDefault(c => {
return c.Type.Equals(StringConstants.ClaimTypes_IdentityProvider, StringComparison.Ordinal) &&
c.Value == StringConstants.Claim_IdentityProvider;
}) != null;
});
//if none found, default to the first identity
if (primaryIdentity == null) return identities.First();
return primaryIdentity;
}
PrimaryIdentitySelector
只有一个时似乎没有运行
Identity
在
Identities
列表。这会导致登录页面出现问题,有时浏览器会在加载页面时传递 WindowsIdentity,但不会在登录请求时传递它 {exasperated sigh}。为了解决这个问题,我最终为登录页面创建了一个 ClaimsIdentity,然后手动覆盖线程的 Principal,如
this SO question 中所述。 .
OnAuthenticate
不会发送 401 来请求 Windows 标识。要解决此问题,您必须注销登录身份。如果登录失败,请确保重新创建登录用户。 (您可能还需要重新创建 CSRF token )
关于c# - User.Identity 在 ClaimsIdentity 和 WindowsIdentity 之间波动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50236994/
我正在尝试检查Powerhell脚本是否以管理员身份运行。 在网上搜索后,我得到了一些运行良好的示例代码。 为了获得WindowsPrincipal对象,我发现了以下两个示例代码。 第一: New-O
我正在尝试使用 SID-s 检查用户是否属于某些组。 我正在使用 WindowsIdentity.Groups ,但后来注意到有时它不显示用户属于管理员组。 搜索了一段时间后,我发现不是Windows
我试图拦截 WindowsAuthenticationModule 以另一个用户身份登录到应用程序,但我无法在不知道他的密码的情况下创建新的 WindowsIdentity。该代码将在管理员权限下运行
我正在开发具有以下详细信息的客户端服务器应用程序: Client(C++) -----------HTTPS--- Server (C#/WCF) 服务器: 与 Windows Active
当我像这样使用 WindowsIdentity 模拟时: WindowsIdentity newId = null; WindowsImpersonationContext impe
我正在构建一个站点,该站点使用当前用户的 WindowsIdentity 获取用户信息。我从中获得的主要信息是 ssid。 我按如下方式为当前用户执行此操作 IntPtr logonToken = W
ServiceSecurityContext 为何包含 WindowsIdentity 属性? 即,如果用户使用 Windows 身份验证机制进行身份验证,则 ServiceSecurityConte
获取当前用户的用户名和/或 SID 时,首选方法是什么? 是否使用: WindowsIdentity.GetCurrent().Owner 或者: WindowsIdentity.GetCurrent
我正在尝试在我的 WPF 客户端/服务器 (WCF) 应用程序中使用 Windows Identity Foundation 进行授权,该应用程序可能会或可能不会在与提供身份验证的事件目录相同的信任环
我正在一个安全的服务器上运行一个应用程序(我必须远程访问并且必须通过电子邮件将文件发送给某人以便他们将它们放到服务器上,因此更改代码不是一个快速的过程)。 在某一时刻,该服务通过获取登录用户 Oper
我有一个由 IIS 托管的 Web 应用程序。它配置了表单例份验证和匿名身份验证,并且启用了模拟。应用程序池帐户是网络服务。匿名帐户是 Costa。 Costa 可以访问数据库。 NetworkSer
假设我有一个 WindowsIdentity 的实例,并且想要获取它所属的组。我使用以下代码获取列表: WindowsIdentity identity = null; // get iden
我正在使用模拟在连接到域的 Windows 7 计算机上实例化 WindowsIdentity。我通过 Groups 属性取回了很多组。一些域、本地和内置但我没有得到用户所在的内置管理员组。我很困惑为
我正在使用 WindowsIdentity 获取当前用户的 ID 以执行 SSO。在大多数情况下,我得到的正是我想要的,但对于某些用户来说,我得到的结果很奇怪。示例代码: IIdentity WinI
TL;DR WindowsIdentity 的 Token 属性中是否包含用户 token (例如,someIdentity.Token) 被欺骗: var validated = new Windo
我有以下代码: var baseUrl = "https://" + GetIdentityProviderHost(environment) + "/oauth2/authorize"; var q
我正在使用 Windows 身份验证的 ASP.NET 应用程序中。 我正在使用 HttpContext.Current.User.Identity.Name 获取当前经过身份验证的用户的用户名,这会
我有一个安装项目(在 Windows 7 上运行)在提交时启动自定义操作,启动刚刚安装的应用程序。在此应用程序启动期间,我有一个方法可以检查当前用户名以执行某些身份验证。当从此自定义操作启动时,我得到
如何从收到的msmq消息中获取发件人的WindowsIdentity? 我使用 msmq 作为传输和安全应用程序 block ,并使用授权规则提供程序进行操作授权。我需要 WindowsPrincip
我有一个 ASP.NET 网站,它要求我的应用程序池是经典 .Net 应用程序池。该站点在 IIS 7 上的 .NET 3.5 上运行。当我尝试获取已登录用户的 Active Directory 用户
我是一名优秀的程序员,十分优秀!