Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 脚手架上的MVC防伪令牌错误_C#_Asp.net Mvc_Asp.net Identity_Owin_Claims Based Identity - Fatal编程技术网

C# 脚手架上的MVC防伪令牌错误

C# 脚手架上的MVC防伪令牌错误,c#,asp.net-mvc,asp.net-identity,owin,claims-based-identity,C#,Asp.net Mvc,Asp.net Identity,Owin,Claims Based Identity,我收到以下错误: {"提供的ClaimsEntity上不存在类型为“”或“”的声明。若要使用基于声明的身份验证启用防伪令牌支持,请验证配置的声明提供程序是否在其生成的ClaimsEntity实例上提供了这两个声明。如果配置的声明提供程序改为使用其他声明类型作为身份验证唯一标识符,可以通过设置静态属性AntiForgeryConfig.UniqueClaimTypeIdentifier来配置它。“} 我试过了,但没有成功 错误正在发生 @Html.AntiForgeryToken() 通用启动.

我收到以下错误:

{"提供的ClaimsEntity上不存在类型为“”或“”的声明。若要使用基于声明的身份验证启用防伪令牌支持,请验证配置的声明提供程序是否在其生成的ClaimsEntity实例上提供了这两个声明。如果配置的声明提供程序改为使用其他声明类型作为身份验证唯一标识符,可以通过设置静态属性AntiForgeryConfig.UniqueClaimTypeIdentifier来配置它。“}

我试过了,但没有成功

错误正在发生

@Html.AntiForgeryToken()
通用启动.cs

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        AuthConfig.ConfigureAuth(app);
    }
}
管理员控制器登录方法

[HttpPost]
public ActionResult Login(Models.AdminUserLogin LoginModel)
{
    if (ModelState.IsValid)
    {
        if (isUserValid(LoginModel.EmailAddr, LoginModel.Password))
        {
            List<Claim> claims = new List<Claim>
            {
                new Claim(ClaimTypes.Email, LoginModel.EmailAddr),
               //some other claims
            };

            ClaimsIdentity identity = new ClaimsIdentity(claims, AuthConfig.DefaultAuthType);
            IAuthenticationManager authManager = Request.GetOwinContext().Authentication;
            authManager.SignIn(new AuthenticationProperties() { IsPersistent = true }, identity);

            return RedirectToAction("Manage");
        }
        else
        {
            ModelState.AddModelError("", "Username and/or password incorrect");
        }
    }
    return View(LoginModel);
}
[HttpPost]
公共操作结果登录(Models.AdminUserLogin登录模型)
{
if(ModelState.IsValid)
{
if(isUserValid(LoginModel.EmailAddr,LoginModel.Password))
{
清单索赔=新清单
{
新索赔(ClaimTypes.Email、LoginModel.EmailAddr),
//其他一些主张
};
ClaimsIdentity identity=newclaimsidentity(claims,AuthConfig.DefaultAuthType);
IAAuthenticationManager authManager=Request.GetOwinContext().Authentication;
SignIn(新的AuthenticationProperties(){IsPersistent=true},标识);
返回重定向操作(“管理”);
}
其他的
{
AddModelError(“,”用户名和/或密码不正确”);
}
}
返回视图(LoginModel);
}

如果您有任何想法,我们将不胜感激。

您的
索赔中需要这两项索赔才能使防伪令牌发挥作用:

List<Claim> claims = new List<Claim>
{
    // adding following 2 claim just for supporting default antiforgery provider
    new Claim(ClaimTypes.NameIdentifier, LoginModel.EmailAddr),
    new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider", "ASP.NET Identity", "http://www.w3.org/2001/XMLSchema#string"),

    // your other claimes
    new Claim(ClaimTypes.Email, LoginModel.EmailAddr),
    //some other claims
};
List claims=新列表
{
//添加以下2个声明仅用于支持默认的反伪造提供商
新声明(ClaimTypes.NameIdentifier、LoginModel.EmailAddr),
新索赔(”http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider“,”ASP.NET标识“,”http://www.w3.org/2001/XMLSchema#string"),
//你的其他要求
新索赔(ClaimTypes.Email、LoginModel.EmailAddr),
//其他一些主张
};

尝试删除AuthConfig.ConfigureAuth(应用程序);然后看看是什么happens@ScottSelby删除会使所有授权页面显示未经授权的错误。是否可以显示“ConfigureAuth”方法?我在VS2013中创建了新的基本项目,在ConfigureAuth中添加标准OWIN,并使用“app.UseCookieAuthentication(…)并添加您的post逻辑登录,一切正常。我使用的软件包有:“Microsoft.Owin.Security”、“Microsoft.Owin”和“Microsoft.Owin.Security.Cookies”。我已经添加了这两个声明,但我得到了以下错误。System.Web.Mvc.HttpAntiForgeryException:“提供的防伪令牌是针对与当前用户不同的基于声明的用户的。”如果我添加了
DefaultNameClaimType
以及
AntiForgeryConfig.UniqueClaimTypeIdentifier=ClaimSidenty.DefaultNameClaimType