我需要使用Azure AD作为身份提供程序将SAML身份验证添加到我的Web应用程序(WebAPI后端/ Angular前端)中。
我计划使用Sustainsys.Saml2库,但不确定如何正确使用该库提供的方法。
我已经将Web应用程序添加为Azure AD上的企业应用程序,并执行了必要的SSO SAML配置。
我已经在web.config中配置了sustainsys,如下所示:
<sustainsys.saml2 entityId="https://myWebApp/api/saml/login" returnUrl="https://myWebApp/">
<identityProviders>
<add
entityId="https://sts.windows.net/36tg486z-9l1f/"
signOnUrl="https://login.microsoftonline.com/36tg486z-9l1f/saml2"
metadataLocation="https://login.microsoftonline.com/36tg486z-9l1f/federationmetadata/2007-06/federationmetadata.xml?appid=82fc2g56-2as2"
allowUnsolicitedAuthnResponse="true"
loadMetadata="true"
binding="HttpPost"
>
</add>
</identityProviders>
<serviceCertificates>
<add fileName="~/App_Data/Sustainsys.Saml2.Tests.pfx" />
</serviceCertificates>
</sustainsys.saml2>
我的想法是编写两个API:
1-执行登录Azure AD的API:
[HttpGet]
[Route("api/saml/signIn")]
[ResponseType(typeof(void))]
public HttpResponseMessage SignIn()
{
var context = new HttpContextWrapper(HttpContext.Current);
HttpRequestBase request = context.Request;
var opt = Sustainsys.Saml2.Configuration.Options.FromConfiguration;
var result = CommandFactory.GetCommand(CommandFactory.SignInCommandName).Run(request.ToHttpRequestData(), opt);
var response = Request.CreateResponse(result.HttpStatusCode);
if (!string.IsNullOrEmpty(result.SetCookieName))
{
var protectedData = HttpRequestData.ConvertBinaryData(MachineKey.Protect(result.GetSerializedRequestState(),HttpRequestBaseExtensions.ProtectionPurpose));
var cookie = new CookieHeaderValue(result.SetCookieName, protectedData) { HttpOnly = true };
var cookies = new List<CookieHeaderValue>() { cookie };
response.Headers.AddCookies(cookies);
}
response.Headers.Location = result.Location;
return response;
}
2-从Azure AD接收响应并读取SAML令牌内的声明的API:
[HttpPost]
[Route("api/saml/acs")]
[ResponseType(typeof(result))]
public HttpResponseMessage Acs()
{
var context = new HttpContextWrapper(HttpContext.Current);
HttpRequestBase request = context.Request;
var opt = Sustainsys.Saml2.Configuration.Options.FromConfiguration;
var result = CommandFactory.GetCommand(CommandFactory.AcsCommandName).Run(request.ToHttpRequestData(), opt);
// ... read claims ...
// ... build [LoginResult] with claims ...
var response = Request.CreateResponse(result.HttpStatusCode, [LoginResult]);
response.Headers.Location = new Uri(result.Location.AbsoluteUri);
return response;
}
这是使用SustainSys添加SAML身份验证的正确方法吗?
答案 0 :(得分:0)
这是使用SustainSys存根Idp的WebApi + Angular Spa的工作示例。它也可以与Azure AD配置一起使用。