加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – MVC5认证中的“和主域之间的信任关系失败”

发布时间:2020-12-16 00:36:50 所属栏目:asp.Net 来源:网络整理
导读:我有一个ASP.NET MVC5应用程序,其中我不使用Windows身份验证。 一切都运行正常,直到我尝试运行应用程序在它正在开发的域之外(无论什么原因)得到: The trust relationship between this workstation and the primary domain failed. 当我试图做User.IsInRo
我有一个ASP.NET MVC5应用程序,其中我不使用Windows身份验证。

一切都运行正常,直到我尝试运行应用程序在它正在开发的域之外(无论什么原因)得到:

The trust relationship between this workstation and the primary domain failed.

当我试图做User.IsInRole(“管理”)。

我正在使用.NET的Identity中的自定义身份,角色,IdentityStore,RoleStore等,我可以看到正确地从(MongoDB)数据库检索到用户和角色数据。

有关这个问题的很多问题,但他们来自想要使用Windows认证的人。并在他们的MVC应用程序中冒充:

> With windows authentication,The trust relationship between the primary domain and the trusted domain failed,when calling IsInRole
> How to configure Windows Authentication / Impersonation + IIS 7 + MVC
> The trust relationship between the primary domain and the trusted domain failed
> My.User.IsInRole(“Role Name”) throws a Trust Relationship error on Windows 7

那么为什么我得到这个SystemException如果我没有使用Active Directory和(据我所知)没有做任何可能依赖于PC的域名?我错过了一些配置(在我的Web.config或IIS Express中)?

编辑:

好的,把它缩小一点…

我的User.IsInRole(“Admin”)行位于我的_Layout.cshtml视图中的if()语句里面(即根据角色知道在nav。bar中显示的内容)。

我现在知道,当没有用户进行身份验证时,我只收到上面的错误,而且我不在我用于开发者的域中。如果我在该行放置一个断点,我可以看到User对象是一个System.Security.Principal.WindowsIdentity,其底层的Identity是System.Security.Principal.WindowsIdentity。

另一方面,如果用户被认证,那么User对象和ts Identity就是System.Security.Claims.ClaimsPrincipal和System.Security.Claims.ClaimsIdentity。

为什么会使用Windows身份(当未经身份验证),如何禁用它?

解决方法

所以,根据我的编辑,我修改了我的_Layout.cshtml,而不是拥有
@if(User.IsInRole("Admin"))  {...}

我有

@if(User.Identity.IsAuthenticated && User.IsInRole("Admin")) {...}

这似乎解决了这个问题。

我相信问题是,当没有用户被认证时,ASP.NET身份使用了empty WindowsIdentity,当我尝试检查User.IsInRole时,那么它将尝试检查WindowsIdentity对Active Directory的角色,我不有。显然,我应该首先检查用户是否甚至登录,然后尝试检查其角色,所以mea culpa。

但是,即使上面的更改似乎修复了我的代码,我将非常有兴趣了解更多关于此行为:为什么当没有用户被认证时,它使用一个空的System.Security.Principal.WindowsIdentity。我会接受任何解释的答案。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读