【发布时间】:2019-03-06 14:23:02
【问题描述】:
是否有人为仅 ASP.NET Core WebAPI 项目(无 Mvc)提供 Sustainsys Saml2 库的工作示例,没有 ASP 身份更重要的是什么? github上提供的示例强烈依赖MVC和SignInManager,我不需要也不想使用。
我添加了 Saml2 身份验证,起初它在我的 IdP 上运行良好(我还检查了 Sustainsys 提供的 StubIdP),所以:
- IdP 元数据已正确加载
- 我的 API 正确重定向到登录页面
- 登录页面重定向到 /Saml2/Acs 页面,我在日志中看到它成功解析结果
但是我不知道如何从那里继续并提取用户登录和其他声明(我的 IdP 还提供了一封电子邮件,它包含在我在日志中确认的 SAML 响应中)。
按照在网上找到的一些示例并修改了一些来自 GitHub 的 MVC 示例,我做了以下操作:
在 Startup.cs 中:
...
.AddSaml2(Saml2Defaults.Scheme,
options =>
{
options.SPOptions.EntityId = new EntityId("...");
options.SPOptions.ServiceCertificates.Add(...));
options.SPOptions.Logger = new SerilogSaml2Adapter();
options.SPOptions.ReturnUrl = new Uri(Culture.Invariant($"https://localhost:44364/Account/Callback?returnUrl=%2F"));
var idp =
new IdentityProvider(new EntityId("..."), options.SPOptions)
{
LoadMetadata = true,
AllowUnsolicitedAuthnResponse = true, // At first /Saml2/Acs page throwed an exception that response was unsolicited so I set it to true
MetadataLocation = "...",
SingleSignOnServiceUrl = new Uri("...") // I need to set it explicitly because my IdP returns different url in the metadata
};
options.IdentityProviders.Add(idp);
});
在 AccountContoller.cs 中(我尝试遵循how to implement google login in .net core without an entityframework provider 中描述的类似情况):
[Route("[controller]")]
[ApiController]
public class AccountController : ControllerBase
{
private readonly ILog _log;
public AccountController(ILog log)
{
_log = log;
}
[HttpGet("Login")]
[AllowAnonymous]
public IActionResult Login(string returnUrl)
{
return new ChallengeResult(
Saml2Defaults.Scheme,
new AuthenticationProperties
{
// It looks like this parameter is ignored, so I set ReturnUrl in Startup.cs
RedirectUri = Url.Action(nameof(LoginCallback), new { returnUrl })
});
}
[HttpGet("Callback")]
[AllowAnonymous]
public async Task<IActionResult> LoginCallback(string returnUrl)
{
var authenticateResult = await HttpContext.AuthenticateAsync(Constants.Auth.Schema.External);
_log.Information("Authenticate result: {@authenticateResult}", authenticateResult);
// I get false here and no information on claims etc.
if (!authenticateResult.Succeeded)
{
return Unauthorized();
}
// HttpContext.User does not contain any data either
// code below is not executed
var claimsIdentity = new ClaimsIdentity(Constants.Auth.Schema.Application);
claimsIdentity.AddClaim(authenticateResult.Principal.FindFirst(ClaimTypes.NameIdentifier));
_log.Information("Logged in user with following claims: {@Claims}", authenticateResult.Principal.Claims);
await HttpContext.SignInAsync(Constants.Auth.Schema.Application, new ClaimsPrincipal(claimsIdentity));
return LocalRedirect(returnUrl);
}
TLDR:我的 ASP.NET Core WebApi 项目中的 SAML 配置看起来不错,并且我在日志中检查了正确的声明并获得了成功响应。我不知道如何提取这些数据(返回 url 错误或者我的回调方法应该以不同的方式工作)。此外,令人费解的是,为什么从 SSO 登录页面成功重定向会被视为“不请自来”,也许这就是问题所在?
感谢您的帮助
【问题讨论】:
-
很高兴你能够让它工作。我在这里很难适应我的应用程序。你会展示你如何配置这个“常量”对象吗?你为“应用程序”和“外部”设置了什么?
-
这些等于简单的字符串“Application”和“External”,仅此而已。
-
"Application" 是你的应用程序的 URL 吗?
-
不,文字字符串“应用程序”和“外部”。这些只是简单字符串模式的标识符。我遵循了本文中描述的模式:stackoverflow.com/questions/53654020/…
-
您是如何配置 SpO.EntityId 参数的?我的 AD FS 服务器日志和错误提示“无法找到标识符为 'localhost:3000/Saml2' 的依赖方信任”。我需要实现该路线吗?是否应该在后端处理?
标签: docker asp.net-core asp.net-core-webapi sustainsys-saml2