asp.Net   发布时间:2022-04-07  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了asp.net-mvc – MVC 5 OWIN登录声明和AntiforgeryToken.我错过了ClaimIdentity提供者吗?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试学习MVC 5 OWIN登录声明.我尽量保持简单.我从MVC模板开始,插入了我的索赔代码(见下文).当我在View中使用@ Html.AntiForgeryToken()帮助器时,我收到一个错误.

错误:

A claim of type 'http://scheR_625_11845@as.xmlsoap.org/ws/2005/05/identity/claims/namEIDentifier' or  
'http://scheR_625_11845@as.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://scheR_625_11845@as.xmlsoap.org/ws/2005/05/identity/claims/namEIDentifier' or 
'http://scheR_625_11845@as.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",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://scheR_625_11845@as.xmlsoap.org/ws/2005/05/identity/claims/namEIDentifier' was 
not present on the provided ClaimsIdentity.

我想保留antiorgeryToken,因为它可以帮助跨站点脚本.

解决方法

您的声明身份没有ClaimTypes.NamEIDentifier,您应该在声明数组中添加更多内容:
var claims = new List<Claim>
{
    new Claim(ClaimTypes.Name,"username"),"user@gmail.com"),new Claim(ClaimTypes.NamEIDentifier,"userId"),//should be userid
};

要将信息映射到索赔以获得更多的纠正:

ClaimTypes.Name => map to username
ClaimTypes.NamEIDentifier => map to user_id

由于用户名也是唯一的,所以您可以使用用户名进行防伪令牌支持.

大佬总结

以上是大佬教程为你收集整理的asp.net-mvc – MVC 5 OWIN登录声明和AntiforgeryToken.我错过了ClaimIdentity提供者吗?全部内容,希望文章能够帮你解决asp.net-mvc – MVC 5 OWIN登录声明和AntiforgeryToken.我错过了ClaimIdentity提供者吗?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。