I am using the token authentication (instead of cookie) with Azure Active Directory.
Based on this article: https://www.itunity.com/article/angular-2-openid-connect-azure-active-directory-3093
I was able to get it working on the client side.
public validateSignature(token): Observable<boolean> {
/* Retrieve from federated metadata endpoint.
In this sample, the document was downloaded locally */
return this.httpService.get("metadata/metadata.xml")
.map((res: Response) => {
let dom = (new DOMParser()).parseFromString(res.text(), "text/xml");
let json = xml2json(dom, "");
let cert = "-----BEGIN CERTIFICATE-----" +
JSON.parse(json).EntityDescriptor[0]["ds:Signature"]
["KeyInfo"]["X509Data"]["X509Certificate"] +
"-----END CERTIFICATE-----";
let key = KEYUTIL.getKey(cert);
return KJUR.jws.JWS.verifyJWT(token, key, { alg: ['RS256'] });
})
}
I was trying to re-implement the above method in the .NET Core 1.0.3.
Based on this article: how to sign and verify signature with net and a certificate
The following line won't compile on .NET Core:
RSACryptoServiceProvider csp = (RSACryptoServiceProvider)cert.PublicKey.Key;
I am not sure what is correct way to verify the token based on the certificate in .NET Core.
An easy way to verify the token issued by Azure AD is leverage the OWIN comment with web API. We just need to config the
JwtBearerOptions
and send the request to a controller which protected by Azure AD. If the token is not verified, you will get the 401 response. You can refer the code sample here.And if you want to implement the code to verify the token manually, we can refer the code how the Microsoft verify the token in Microsoft.AspNetCore.Authentication.JwtBearer.
I also wrote a code sample for your reference:
Project.json
According to this QA: implement RSA in .NET core your
cert
object should have aGetRSAPublicKey()
method which returns anRSA
object - just be sure to wrap it inusing
as it'sIDisposable
.Apparently
GetRSAPublicKey()
is defined as an extension method: https://msdn.microsoft.com/en-us/library/system.security.cryptography.x509certificates.rsacertificateextensions.getrsapublickey(v=vs.110).aspx