【问题标题】:OnAuthenticationCompleted in ASP.NET 5 RCOnAuthenticationCompleted 在 ASP.NET 5 RC
【发布时间】:2016-02-26 03:22:01
【问题描述】:

我有一个 ASP.NET 5 beta 8 应用程序,它使用 OpenIdConnect 与 Azure Active Directory 集成。我尝试将应用程序更新到 RC1 并将 openid nuget 包更改为"Microsoft.AspNet.Authentication.OpenIdConnect": "1.0.0-rc1-final"。一切似乎都是正确的,但是我用来向我的ClaimsIdentity 添加角色的OnAuthenticationComplete 方法不再在Events 对象中,我找不到替代方法。如何使用新版本向我的身份添加声明?

更新:更改为 OnAuthenticationValidated 仍然不适合我。我一定是在我的活动代码中做错了什么:

OnAuthenticationValidated = async notification =>
{
    var claimsIdentity = notification.AuthenticationTicket.Principal.Identity as ClaimsIdentity;
    var userRepository=new UserRepository(Configuration);
    var userId = claimsIdentity?.FindFirst(ClaimTypes.NameIdentifier)?.Value;
    var user = await userRepository.FindAsync(userId);
    if (user == null)
    {
        await userRepository.AddAsync(new UserDto
        {
            UserId = userId,
            Username = claimsIdentity?.FindFirst(ClaimTypes.Name)?.Value,
            DisplayName = claimsIdentity?.FindFirst(ClaimTypes.Name)?.Value
        });
    }
    claimsIdentity?.AddClaim(new Claim(ClaimTypes.Role, "super"));
}

另外,我用来登录的代码是:

[HttpGet]
public IActionResult Login()
{
    if (HttpContext.User == null || !HttpContext.User.Identity.IsAuthenticated)
        return new ChallengeResult(OpenIdConnectDefaults.AuthenticationScheme, new AuthenticationProperties { RedirectUri = "/" });
    return RedirectToAction("Index", "Home");
}

【问题讨论】:

    标签: asp.net-core asp.net-core-mvc azure-active-directory openid-connect


    【解决方案1】:

    AuthenticationCompleted 事件已重命名为 AuthenticationValidated

    您可以找到有关此票证的更多信息:https://github.com/aspnet/Security/pull/442

    【讨论】:

    • 这是我的第一个想法,但它不起作用。我一定做错了什么。我会用我的事件代码更新问题。
    • “不工作”有点含糊。事件是否触发?您是否尝试过启用跟踪(在详细级别)以确定内部发生了什么?
    • 事件未触发。我去了登录页面,但它没有调用事件就回来了。我尝试回滚到 beta 8,它可以工作。在更新到 RC1 时,我只更改了事件的名称并删除了 options.DefaultToCurrentUriOnRedirect = true;,因为该属性不存在并且不再起作用。
    • 有趣。如果您使用TicketReceived 事件而不是AuthenticationValidated,它是否有效?
    • 不,它不会被调用。我想我会从头开始这部分(没有太多,所以它可能比盲目地改变事情更好......)。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-18
    • 1970-01-01
    • 2010-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多