4

我正在评估Kentor 身份验证服务(它的 OWIN 版本)以使用 SAML 对用户进行身份验证。现在我想向服务传递一个额外的声明。连同那里的示例,我能够将请求发送到服务并对其进行调试。

我做了一个自定义的 claimAuthenticationManager ,在那里我可以看到附加的声明到达 auth 服务。但后来(在 Kendor 示例中,有一个视图主页/索引列出了所有声明)此声明不再可用。有谁知道我做错了什么?

非常感谢!

4

1 回答 1

5

将 AuthServices(或任何外部登录)与 ASP.NET Identity 一起使用时,传入的声明仅用于在数据库中查找 ASP.NET Identity 用户。然后传入用户被完全丢弃,来自 ASP.NET Identity 的用户被加载并使用

在默认的 MVC5 模板中,从外部标识到 ASP.NET 标识的切换是在AccountController.ExternalLoginCallback(). 要保留传入的信息,您必须调整此方法。有两种选择。

1.更新存储的用户ExternalLoginCallback()

// Sign in the user with this external login provider if the user already has a login
var user = await UserManager.FindAsync(loginInfo.Login);
if (user != null)
{
  // Update user with info from external identity and save.
  user.GivenName = loginInfo.ExternalIdentity.FindFirst(ClaimTypes.GivenName).Value;
  await UserManager.UpdateAsync(user);

  await SignInAsync(user, isPersistent: false);
  return RedirectToLocal(returnUrl);
}

2. 仅将传入声明用于当前会话。

将 的内容复制SignInAsync()到您的ExternalLoginCallback()方法中。提取对 user.GenerateUserIdentityAsync() to a separate line and. Add claims before callingSignInAsync() 的调用`

// Sign in the user with this external login provider if the user already has a login
var user = await UserManager.FindAsync(loginInfo.Login);
if (user != null)
{
  AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
  var identity = await user.GenerateUserIdentityAsync(UserManager);
  identity.AddClaim(loginInfo.ExternalIdentity.FindFirst(ClaimTypes.GivenName));
  AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent },
    identity);

  return RedirectToLocal(returnUrl);
}

建议

也可以在没有 ASP.NET Identity 的情况下使用外部登录。如果您只使用来自 Idp 的身份而不使用其他登录方法,那可能更容易使用。

于 2014-11-20T21:22:34.707 回答