首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误AntiForgeryToken C# MVC

错误AntiForgeryToken C# MVC
EN

Stack Overflow用户
提问于 2017-06-27 01:15:24
回答 1查看 545关注 0票数 0

我在我的tesis上工作,这是一个使用MVC和EntitieFrameworks,C#和Visual Studio的项目。我使用通过sql server 2016获得的模型中的实体来创建控制器和视图。对于这一点,一切都很好。我的cshtml中的AntiForgeryToken属性出现了问题,导致出现了这个错误。

代码语言:javascript
复制
 claim of type 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier' was not present on the provided ClaimsIdentity.

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

那么,为什么要添加这个呢?这是框架自动生成的代码,我不知道为什么会出现这个错误。你能帮我吗?我的cshtml以

代码语言:javascript
复制
@model Entities.Producto

@{ViewBag.Title = "Crear Producto";}

<h2>Nuevo producto</h2>


@using (Html.BeginForm()) 
{
       @Html.AntiForgeryToken()

和我的控制器

代码语言:javascript
复制
 namespace Web.Controllers
{
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Entities;
using Services;
using Web.Models;


public class ProductoController : BaseController
{
    IProductoService productoService;
    IRubroService rubroService;
    IProveedorService proveedorService;

    public ProductoController(IProductoService productoService, IRubroService rubroService, IProveedorService proveedorService)
    {
        this.productoService = productoService;
        this.rubroService = rubroService;
        this.proveedorService = proveedorService;
    }
    // GET: Producto
    public ActionResult Index()
    {
        List<Producto> productos = this.productoService.ObtenerProductos();

        List<Producto> productosVM = new List<Producto>();
        foreach (Producto producto in productos)//.Where(x => x.Activo == true))
        {
            if (producto.IdRubro != null)
            {
                producto.Rubro = this.rubroService.ObtenerRubro(producto.IdRubro.Value);
            }
            if (producto.IdProveedor != null)
            {
                producto.Proveedor = this.proveedorService.ObtenerProveedor(producto.IdProveedor.Value);
            }
            productosVM.Add(producto);
        }
        return View(productosVM);
    }

    // GET: Producto/Details/5
    public ActionResult Details(int? id)
    {
        Producto producto = this.productoService.ObtenerProducto(id.Value);
        if (producto == null)
            return HttpNotFound();


        return View(producto);
    }

    // GET: Producto/Create
    public ActionResult Create()
    {
        ViewBag.TiposRubro = new SelectList(this.rubroService.ObtenerRubros(), "Id", "Nombre");
        ViewBag.TiposProveedor = new SelectList(this.rubroService.ObtenerRubros(), "Id", "Nombre");
        return View();
    }

    // POST: Producto/Create
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "Id,Descripcion,Codigo,Cantidad,Costo,Precio,Fecha,IdRubro,IdProveedor,StockMinimo,Activo,StockMaximo")] Producto producto)
    {
        try
        {
            if (ModelState.IsValid)
            {
                if (this.productoService.ObtenerProducto(producto.Id) == null)
                {
                    producto.FechaModificacion = DateTime.Today;
                    this.productoService.Crear(producto);
                    this.Success("Producto creado correctamente.");
                    return RedirectToAction("Index");
                }
                else
                {
                    this.Information("El producto ya existe.");
                }
            }
        }
        catch (Exception ex)
        {
            ViewBag.IdProveedor = new SelectList(this.proveedorService.ObtenerProveedores(), "Id", "Nombre", producto.IdProveedor);
            ViewBag.IdRubro = new SelectList(this.rubroService.ObtenerRubros(), "Id", "Nombre", producto.IdRubro);
            this.Error("Hubo un error al crear el producto");
            return View(producto);
        }

        ViewBag.IdProveedor = new SelectList(this.proveedorService.ObtenerProveedores(), "Id", "Nombre", producto.IdProveedor);
        ViewBag.IdRubro = new SelectList(this.rubroService.ObtenerRubros(), "Id", "Nombre", producto.IdRubro);
        return View(producto);
    }

    // GET: Producto/Edit/5
    public ActionResult Edit(int id)
    {
        Producto producto = this.productoService.ObtenerProducto(id);

        if (producto == null)
        {
            return HttpNotFound();
        }

        ViewBag.IdProveedor = new SelectList(this.proveedorService.ObtenerProveedores(), "Id", "Nombre", producto.IdProveedor);
        ViewBag.IdRubro = new SelectList(this.rubroService.ObtenerRubros(), "Id", "Nombre", producto.IdRubro);
        return View(producto);
    }

    // POST: Producto/Edit/5
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit(Producto producto)
    {
        try
        {
            if (ModelState.IsValid)
            {
                producto.FechaModificacion = DateTime.Now;
                this.productoService.Actualizar(producto);
                //this.Success("Producto actualizado con exito");
                return RedirectToAction("Index");
            }

            ViewBag.IdProveedor = new SelectList(this.proveedorService.ObtenerProveedores(), "Id", "Nombre", producto.IdProveedor);
            ViewBag.IdRubro = new SelectList(this.rubroService.ObtenerRubros(), "Id", "Nombre", producto.IdRubro);
            this.productoService.Actualizar(producto);
            this.Error(ModelState.Values.First().Value.Culture.Calendar.AlgorithmType.ToString());
            return RedirectToAction("Index");
        }
        catch (Exception ex)
        {
            ViewBag.IdProveedor = new SelectList(this.proveedorService.ObtenerProveedores(), "Id", "Nombre", producto.IdProveedor);
            ViewBag.IdRubro = new SelectList(this.rubroService.ObtenerRubros(), "Id", "Nombre", producto.IdRubro);
            this.Error("Hubo un error al actualizar el producto");
            return RedirectToAction("Index");
        }
    }

    // GET: Producto/Delete/5
    public ActionResult Delete(int? id)
    {
        Producto producto = this.productoService.ObtenerProducto(id.Value);

        if (producto == null)
        {
            return HttpNotFound();
        }
        return View(producto);
    }

    // POST: Producto/Delete/5
    [HttpPost, ActionName("Delete")]
    [ValidateAntiForgeryToken]
    public ActionResult DeleteConfirmed(int id)
    {
        this.productoService.Borrar(id);
        return RedirectToAction("Index");
    }
}

}

谢谢!!

EN

回答 1

Stack Overflow用户

发布于 2020-02-06 21:19:23

使用以下代码检查并更新您的Global.asax.cs文件,您需要添加AntiForgeryConfig.

命名空间您的项目名称{公共类名称: RouteConfig.RegisterRoutes(RouteTable.Routes);{ protected void MvcApplication (){ AreaRegistration.RegisterAllAreas();System.Web.HttpApplication AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.NameIdentifier;}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44765083

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档