On behalf of flow with Custom Connectors for MS Flow
I have always been fascinated by different authentication mechanisms that Microsoft provides and recently when a colleague of mine was working on a custom connector, I got to assist her with setting up On-behalf of Flow authentication.
Custom Connectors are kind of actions provided by 3rd parties that users can use in Microsoft Flows.
First of all you need to create a Web API, that will perform the action when someone uses the custom connector. You will need to host this Web API on Azure and create an AAD application that will register the Web API. You will also need create an AAD app that will register the custom connector. You will also need to establish communication between Web API AAD app and Custom Connector AAD App. Steps for this can be found here, https://hiralpatel-sharepointdevelope.blogspot.com/2019/03/steps-to-create-custom-connector-for.html
Basically, here, our client is our custom connector, which communicates with the AAD app hosted on azure to acquire a access token, which is provided after successful sign on, let's call it Token1, after that Token1 is used to communicate with web api and web api validates the token and requests Token2 to access the resource, Token 2 is silently acquired and used to communicate with the resource.
Here, we are using the initial Bearer token that you can acquire from the HTTP Headers to acquire next token that we can use to access the resource.
string url = HttpContext.Current.Request.Url.AbsoluteUri;
var Headers = HttpContext.Current.Request.Headers;
string assertion = Headers["Authorization"].Replace("Bearer ", "");
string assertionType = "urn:ietf:params:oauth:grant-type:jwt-bearer";
var userAssertion = new UserAssertion(assertion, assertionType, ClaimsPrincipal.Current.FindFirst(ClaimTypes.Upn)?.Value);
string tenantClaim = "http://schemas.microsoft.com/identity/claims/tenantid";
var authContext = new AuthenticationContext(string.Format("https://login.microsoftonline.com/{0}",
ClaimsPrincipal.Current.FindFirst(tenantClaim)?.Value));
var credentails = new ClientCredential("client id of the Web API AAD App", "client secret of connector Web API AAD App");
var result = await authContext.AcquireTokenAsync(resourceUri.Scheme + Uri.SchemeDelimiter + resourceUri.Host, credentails, userAssertion);
The blog that helped us a lot to devise the solution. https://paulryan.com.au/2017/oauth-on-behalf-of-flow-adal/
Custom Connectors are kind of actions provided by 3rd parties that users can use in Microsoft Flows.
First of all you need to create a Web API, that will perform the action when someone uses the custom connector. You will need to host this Web API on Azure and create an AAD application that will register the Web API. You will also need create an AAD app that will register the custom connector. You will also need to establish communication between Web API AAD app and Custom Connector AAD App. Steps for this can be found here, https://hiralpatel-sharepointdevelope.blogspot.com/2019/03/steps-to-create-custom-connector-for.html
Basically, here, our client is our custom connector, which communicates with the AAD app hosted on azure to acquire a access token, which is provided after successful sign on, let's call it Token1, after that Token1 is used to communicate with web api and web api validates the token and requests Token2 to access the resource, Token 2 is silently acquired and used to communicate with the resource.
Here, we are using the initial Bearer token that you can acquire from the HTTP Headers to acquire next token that we can use to access the resource.
string url = HttpContext.Current.Request.Url.AbsoluteUri;
var Headers = HttpContext.Current.Request.Headers;
string assertion = Headers["Authorization"].Replace("Bearer ", "");
string assertionType = "urn:ietf:params:oauth:grant-type:jwt-bearer";
var userAssertion = new UserAssertion(assertion, assertionType, ClaimsPrincipal.Current.FindFirst(ClaimTypes.Upn)?.Value);
string tenantClaim = "http://schemas.microsoft.com/identity/claims/tenantid";
var authContext = new AuthenticationContext(string.Format("https://login.microsoftonline.com/{0}",
ClaimsPrincipal.Current.FindFirst(tenantClaim)?.Value));
var credentails = new ClientCredential("client id of the Web API AAD App", "client secret of connector Web API AAD App");
var result = await authContext.AcquireTokenAsync(resourceUri.Scheme + Uri.SchemeDelimiter + resourceUri.Host, credentails, userAssertion);
The blog that helped us a lot to devise the solution. https://paulryan.com.au/2017/oauth-on-behalf-of-flow-adal/
Comments
Post a Comment