asp.net-mvc - 使用声明和 AntiforgeryToken 的 MVC 5 OWIN 登录。我是否错过了 ClaimIdentity 提供商?

标签 asp.net-mvc asp.net-mvc-4 razor asp.net-mvc-5 claims-based-identity

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

错误:

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, "brockallen@gmail.com")
    };
    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 ( like in this SO answer )

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,因为它可以帮助防止跨站点脚本编写。

最佳答案

在您的 Application_Start() 中,指定要用作 NameIdentifierClaim:

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

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

        ...
    }
}

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

关于asp.net-mvc - 使用声明和 AntiforgeryToken 的 MVC 5 OWIN 登录。我是否错过了 ClaimIdentity 提供商?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30976980/

相关文章:

asp.net-mvc - 更新用户声明未生效。为什么?

c# - 传入字典的模型项是 'System.Data.Entity.Infrastructure.DbQuery` ,但需要 'System.Collections.Generic.IEnumerable

html - PartialViews - 不在编辑器/显示模板中使用共享 Site.css - ASP.NET MVC 3

razor - 如何在 RenderFragment 中定义模板化组件及其 @context 可用

javascript - MVC : Ajax data not getting to Controller

asp.net-mvc - 在 MVC 3 中跨请求保留查询字符串

c# - 从 C# 中的 void 方法返回错误消息

asp.net-mvc - MVC 4 openauth - 自定义数据存储

c# - ASP.Net MVC 4 WebAPI 自定义路由

c# - Paypal 按钮管理器不接受小数金额