使用声明和 AntiforgeryToken 的 MVC 5 OWIN 登录。我是否错过了 ClaimsIdentity 提供商?

2024-03-04

我正在尝试学习 MVC 5 OWIN 登录的声明。我尝试让它尽可能简单。我从 MVC 模板开始并插入我的声明代码(见下文)。当我在视图中使用 @Html.AntiForgeryToken() 帮助程序时,出现错误。

Error:

A claim of type 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier' or  
'http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovid    
er' was not present on the provided ClaimsIdentity. 

To enable anti-forgery token support with claims-based authentication, please verify that 
the configured claims provider is providing both of these claims on the ClaimsIdentity 
instances it generates. If the configured claims provider instead uses a different claim 
type as a unique identifier, it can be configured by setting the static property 
AntiForgeryConfig.UniqueClaimTypeIdentifier.

Exception Details: System.InvalidOperationException: A claim of type
'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier' or 
'http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider' was 
not present on the provided ClaimsIdentity. To enable anti-forgery token
support with claims-based authentication, please verify that the configured claims provider 
is providing both of these claims on the ClaimsIdentity instances it generates. 
If the configured claims provider instead uses a different claim type as a unique 
identifier, it can be configured by setting the static property 
AntiForgeryConfig.UniqueClaimTypeIdentifier.

Source Error:
Line 4:      using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new 
{ id = "logoutForm", @class = "navbar-right" }))
Line 5:      {
Line 6:      @Html.AntiForgeryToken()

POST 登录操作

// POST: /Account/Login
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
    if (!ModelState.IsValid)
    {
        return View(model);
    }

    var claims = new List<Claim>
    {
        new Claim(ClaimTypes.Name, "Brock"),
        new Claim(ClaimTypes.Email, "[email protected] /cdn-cgi/l/email-protection")
    };
    var id = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie);

    var ctx = Request.GetOwinContext();
    var authenticationManager = ctx.Authentication;
    authenticationManager.SignIn(id);

    return RedirectToAction("Welcome");
}

_LoginPartial.cshtml

@using Microsoft.AspNet.Identity
@if (Request.IsAuthenticated)
{
    using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm", @class = "navbar-right" }))
    {
    @Html.AntiForgeryToken()

    <ul class="nav navbar-nav navbar-right">
        <li>
            @Html.ActionLink("Hello " + User.Identity.GetUserName() + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })
        </li>
        <li><a href="javascript:document.getElementById('logoutForm').submit()">Log off</a></li>
    </ul>
    }
}

我尝试过设置ClaimTypes.NameIdentifier (就像在这个SO答案中一样 https://stackoverflow.com/questions/19977833/anti-forgery-token-issue-mvc-5)

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);

    AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.NameIdentifier;
}

然后我“只是?”得到这个错误

A claim of type 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier' was 
not present on the provided ClaimsIdentity.

我想保留 antiforgeryToken,因为它可以帮助防止跨站点脚本编写。


In your Application_Start(),指定哪个Claim用作NameIdentifier:

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        ...

        System.Web.Helpers.AntiForgeryConfig.UniqueClaimTypeIdentifier = 
            System.Security.Claims.ClaimTypes.NameIdentifier;

        ...
    }
}

See: http://brockallen.com/2012/07/08/mvc-4-antiforgerytoken-and-claims/ http://brockallen.com/2012/07/08/mvc-4-antiforgerytoken-and-claims/

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用声明和 AntiforgeryToken 的 MVC 5 OWIN 登录。我是否错过了 ClaimsIdentity 提供商? 的相关文章

随机推荐

  • 在angularjs中使用debounce延迟代码

    我必须在 AngularJS 中编写电子邮件验证功能 当用户使用电子邮件 ID 完成编辑后 我想在 2 秒后发出发布请求 AngularJS 中有没有为此预先定义的方法 fiddle http jsfiddle net Qdk5M 1944
  • 将句子每行一个单词分开

    是否可以使用 CSS 将句子分成每行一个单词 Input div Hello world foo bar div 渲染输出 Hello world foo bar 将宽度设置为例如1px是不希望的 你必须使用display table ca
  • 对于导航堆栈的根视图控制器,isMovingToParentViewController 总是返回 FALSE?

    确定导航堆栈的根视图 顶级 控制器是由于最初呈现还是未被发现而出现的正确方法是什么 iOS 文档建议在 viewWill DidAppear 中使用 isMovingToParentViewController 来做出决定 这适用于推送到堆
  • Django-rest-framework 与 django OAuth 2.0 给出身份验证错误

    我已将 django rest framework 与 django oauth toolkit 集成 它给了我 detail Authentication credentials were not provided 使用未经身份验证的 A
  • AspectJ EDT-Checker 代码问题

    我目前正在使用 Alexander Potochkin 的 AspectJEDT 检查器代码 http weblogs java net blog alexfromsun archive 2006 02 debugging swing ht
  • 在没有 GUI 的服务器上运行 Smalltalk?

    我有一个相当明显的问题 我想在生产服务器上运行 Smalltalk 而不使用图形界面 VW 或 Pharo 甚至 Squeak 可能实现这一点吗 我有一个没有 X 的 VPS 托管 并且希望在 Smalltalk 上运行一些网站 同时使用成
  • emberjs 如何禁用 Ember.Select?

    我有一个与这个问题类似的问题 Emberjs 禁用和启用 TextField https stackoverflow com questions 10523780 emberjs disable and enable textfield 所
  • 如何安全地为 Perl 5.8.9 编译 Perl 5.12 模块?

    我想在我的 Perl 5 8 9 中安装 File Fetch 它是 Perl 5 12 中的核心模块 一般来说 我想在回溯的 Perl 中编译和安装未来的模块 因为我无法升级我的 Perl 所以我下载了该模块及其依赖项 遵循依赖关系树是相
  • 在 django 中建立一对多关系

    我正在尝试在 django 中建立一对多关系 在我的模型中 我有一个 Person 类和一个 Group 类 我想要建立的关系是一个 Group 内部可以有 N 个人 并且一个 Group 不能在没有至少一个人的情况下存在 在 MER 图中
  • 将 Cython 中的 numpy 数组传递给需要动态分配数组的 C 函数

    我有一些具有以下声明的 C 代码 int myfunc int m int n const double a double b double c So a是一个常量二维数组 b是一个二维数组 并且c是一个一维数组 全部动态分配 b and
  • 抛出 VS 重新抛出:结果相同吗?

    参考网上的很多文档 特别是关于SO的文档 例如 在 C 中重新引发异常的正确方法是什么 https stackoverflow com questions 178456 what is the proper way to re throw
  • 使用剃刀时关闭WebFormViewEngine?

    我下载了Glimpse http www getglimpse com 今天早上尝试一下 当我单击 视图 选项卡时注意到这一点 它检查所有加载的视图引擎 我发现在哪里RazorViewEngine在 web config 中指定 但我找不到
  • 如何在收到 fetch POST 请求的响应后将用户重定向到页面?

    我正在为一个 Web 应用程序编写代码 该应用程序使用 javascript 的 fetch api 向 node js 服务器发送 POST 请求 成功请求后 服务器会进行重定向响应 该重定向 URL 将在 fetch api 响应正文中
  • 谷歌智能锁与凭证管理 API

    我想为我的网络应用程序实现无摩擦登录过程 经过一番搜索 我发现有两种解决方案 谷歌智能锁 https developers google com identity one tap web get started 凭证管理API https
  • 如何将 bin 文件(512 字节)写入软盘的第一个扇区(扇区 0)?

    如何将 bin 文件写入软盘 虚拟软盘 软盘映像的第一个扇区 我正在尝试启动一个简单的 512 字节引导加载程序 到处都显示 512 字节 的大小 所以我应该已经很好了 附加信息 引导加载程序仅显示一个字符串 我正在学习simple集会 有
  • 使用 UnityContainer 中的接口注册类型

    我正在使用 UnityContainer 并且我想注册一个接口 而不是使用类型 而是使用另一个接口 不幸的是 我无法干净地做到这一点 我有几个常用的接口 它们统一在一个接口中 我需要将它们注册到容器中 代码如下 interface IDev
  • iPhone清除CGContext

    我用这段代码创建了一个带有漂亮阴影的圆圈 我在 iPhone 上使用 MonoTouch net Objective C 答案当然很好 UIGraphics PushContext ctx SizeF shadowSize new Size
  • 过滤罗盘读数

    我正在使用罗盘标题来旋转 MKMapView 旋转有点不稳定 所以我试图像 iPhone 上的 Google 地图一样过滤它 或者似乎做了一些欺骗 我正在尝试使用移动平均公式过滤 iPhone 指南针的读数 但它在 359 和 0 之间的交
  • Android 服务被杀死

    我有一个服务与我的应用程序在同一进程中运行 有时 Android 操作系统决定终止我的服务 可能是由于内存不足 我的问题是 我的应用程序是否会与服务一起被杀死 或者它到底是如何工作的 Thanks 首先请务必阅读 http develope
  • 使用声明和 AntiforgeryToken 的 MVC 5 OWIN 登录。我是否错过了 ClaimsIdentity 提供商?

    我正在尝试学习 MVC 5 OWIN 登录的声明 我尝试让它尽可能简单 我从 MVC 模板开始并插入我的声明代码 见下文 当我在视图中使用 Html AntiForgeryToken 帮助程序时 出现错误 Error A claim of