I'm trying to use power automate to post into to an azure function app which can then hash the message with the body and header info and send back the value. See below example code.
#r "Newtonsoft.Json"
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using System.Reflection;
using System.Security.Cryptography;
public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
string name = req.Query["name"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;
string responseMessage = string.IsNullOrEmpty(name)
? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
: $"Hello, {name}. This HTTP triggered function executed successfully.";
return new OkObjectResult(responseMessage);
}
public class MyHmac
{
private string CreateToken(string message, string secret)
{
public void DoSomething()
{
string var = Task.name;
}
// message = Task.name;
secret = secret ?? "";
var encoding = new System.Text.ASCIIEncoding();
byte[] keyByte = encoding.GetBytes(secret);
byte[] messageBytes = encoding.GetBytes(message);
using (var hmacsha256 = new HMACSHA256(keyByte))
{
byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
return Convert.ToBase64String(hashmessage);
}
}
}
The first part receives the message and the second part processes the hash. I'm very new to coding and unable to workout how to use variables from the first public class?
CodePudding user response:
Assuming you're trying to pass variables into the MyHmac class and call CreateToken.
Classes are Objects, and within objects you have public a private variables (other objects) and functions (which can take parameters). There's other things within classes, but that's all you need to know to solve this problem.
One issue I see is that CreateToken is a private function (it should be public). Another issue is with string var = Task.name;
(can't use "var" since that's a reserved object type).
In order for your Task<IActionResult> Run
to call a function within MyHmac, you first need to create a MyHmac variable within the "Run" function, and then have the new MyHmac variable call whatever function is in the MyHmac class you want.
ex:
Run(HttpRequest req, ILogger log){
MyHmac localVariable = new MyHmac();
string secret = "example secret string";
string tokenResponseMessage = localVariable.CreateToken("here's a message string", secret);
}
return new OkObjectResult(tokenResponseMessage);
CodePudding user response:
Values are passed to methods as method parameters.
Declare the method as public
so it can be called from other classes, and static
so it can be called without an instance:
public class MyHmac
{
public static string CreateToken(string message, string secret)
{
secret = secret ?? "";
var encoding = new System.Text.ASCIIEncoding();
byte[] keyByte = encoding.GetBytes(secret);
byte[] messageBytes = encoding.GetBytes(message);
using (var hmacsha256 = new HMACSHA256(keyByte))
{
byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
return Convert.ToBase64String(hashmessage);
}
}
}
name = name ?? data?.name;
string token = MyHmac.CreateToken(name, "something secret");
In that last line of code above, the fact that you pass name
in as the value for the first parameter means that CreateToken
will get called with that value in its message
parameter.
Alternatively, you could make it public
but not static
, and create a new MyHmac()
instance to call the method from, as Zeke's answer says.