Create a C# Webhook for Okta

Hi everyone,

This question might be too general but I basically want to create a webhook in c# to respond to
a eventhook in Okta. I understand what needs to be done in Okta (I think) but having trouble getting started on c#. If anyone can help me get started it would be greatly appreciated

Hi @userone! Although not specific to C# we do have an example in Node JS - Build Easy User Sync Webhooks with Okta | Okta Developer.

I just started looking into eventhooks. I build a web api controller (yeh, kind old tech, but it works)

I only got as far as to save the data sent and write it out. The oktainlinehookheader is a random string value i created and saved in web.config and entered into Okta. The ‘HttpPost’ is what is used, but ‘HttpGet’ is what is used for Okta to verify the code is yours.

Hope this helps.

                private string oktaInlineHookHeader = System.Configuration.ConfigurationManager.AppSettings["oktaInlineHookHeader"].ToString();[HttpPost]
    public object Post(HttpRequestMessage request)
    {
        AuthenticationHeaderValue authorization = request.Headers.Authorization;var data = request.Content.ReadAsStringAsync().Result;

        string fn;

        fn = Guid.NewGuid().ToString().Left(5) + "-eventHook.txt";

        
            System.IO.File.WriteAllText(DataFolder + fn, data);

        if (authorization == null || string.IsNullOrEmpty(authorization.Scheme))
        {
           
            return Content(HttpStatusCode.Unauthorized, "Missing credentials");
        }

        if (request.Headers.Contains("Authorization"))
        {
            string a = request.Headers.GetValues("Authorization").First();

            if (a != oktaInlineHookHeader)
            {
                return Content(HttpStatusCode.Unauthorized, "Invalid header");
            }
        }

        JObject o = JObject.Parse(data);

        return Content(HttpStatusCode.OK, "");

    }

    [HttpGet]
    public object Get()
    {
        var re = Request;
        var headers = re.Headers;

        if (headers.Contains("X-Okta-Verification-Challenge"))
        {
            string value = headers.GetValues("X-Okta-Verification-Challenge").First();

            OktaVerify ov = new OktaVerify();
            ov.verification = value;

            return Ok(ov);
        }

        return NotFound();
    }

    public class OktaVerify
    {
        public string verification { get; set; }
    }