Add SAML Authentication to .net WebAPI

房东的猫 提交于 2019-12-24 06:34:30

问题


I need to add SAML Authentication to my web application (WebAPI Back-End / Angular Front-End) with Azure AD as my Identity Provider.

I plan to use Sustainsys.Saml2 library but I'm not sure how to correctly use the methods that the library provide.

I already added my web app as an the Enterprise Application on Azure AD and performed the necessary SSO SAML configurations.

I have configured sustainsys in the web.config as following:

<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>

My idea is to write two APIs:

1 - API that perform the sign in into Azure AD:

[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 - API that receive the response from Azure AD and reads the claims inside the SAML token:

[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;
}

Is this the correct way to add SAML authentication using SustainSys ?

来源:https://stackoverflow.com/questions/55610666/add-saml-authentication-to-net-webapi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!