{"__v":13,"_id":"56c1ffb1d07cc50d005e5f5e","category":{"__v":9,"_id":"56b200c0f48f270d00e0de6f","pages":["56b201173b6f690d00bdb68e","56b20ef3bf040b0d00588be4","56b214097ae4550d00062928","56b21502bf040b0d00588bea","56c1ffb1d07cc50d005e5f5e","56c74a05af5bc40d00d6bb5b","56d412b38001e30b00896dd5","56d85fc1c83c5f0b00d288a2","56d866f4a957fb1500765a40"],"project":"568fce2a04440a1700e4cb47","version":"568fce2b04440a1700e4cb4a","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2016-02-03T13:29:36.476Z","from_sync":false,"order":1,"slug":"c","title":"C# Quickstarts"},"parentDoc":null,"project":"568fce2a04440a1700e4cb47","user":"55116f88e2990b0d00fb0552","version":{"__v":20,"_id":"568fce2b04440a1700e4cb4a","project":"568fce2a04440a1700e4cb47","createdAt":"2016-01-08T14:56:43.101Z","releaseDate":"2016-01-08T14:56:43.101Z","categories":["568fce2b04440a1700e4cb4b","568fd1b8b700ce0d002f4b1c","568fd23804440a1700e4cb5b","568fd2444719c119002ce5d8","568ff21204440a1700e4cbc1","5693732c8aa8040d009f2c28","5693738393445b0d00abdad0","5693740093445b0d00abdad1","56937445974aaa0d001ca699","5693b82173f48f0d0075c90d","5694c4cd1005590d0062cb25","569f854466a5640d00efa54c","56a264cdd15dd70d008d825b","56aa56bf318e6c1700a19ddb","56b0e6347ae4550d000627bd","56b200c0f48f270d00e0de6f","56b200c6f48f270d00e0de70","56b22a9665ddf50d0076ba40","56e92ef71996862200fd7f42","574d6577fb835c0e00ca316a"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2016-02-15T16:41:21.263Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":4,"body":"[block:callout]\n{\n  \"type\": \"info\",\n  \"body\": \"Full source for these quickstarts can be downloaded from http://download.dynmark.com/Files/API/Quickstart/CS.zip\"\n}\n[/block]\n##Prerequisites\nYou'll need a Dynmark account to be able to send messages. You can use a trial account to send a limited number of free messages for testing.\n\n##Registration\nWe're going to create a simple web application that sends a verification code via SMS when the user enters their phone number. We'll make use of the RestSharp library to handle the REST call to the Dynmark API.\n\nFirst we'll build the registration page to ask the user for their phone number. We'll start by defining the Dynmark username and password used to authenticate with the API, and some API response classes.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \":::at:::using System\\n@using System.Collections.Generic\\n@using System.Linq\\n@using RestSharp\\n@using RestSharp.Authenticators\\n@using RestSharp.Deserializers\\n\\n@functions {\\n    // set our Dynmark username and password\\n    string username = \\\"@Username@\\\";\\n    string password = \\\"@Password@\\\";\\n  \\n    public class SendCodeResponse\\n    {\\n        public Guid RequestId { get; set; }\\n        public string To { get; set; }\\n        public string NumberStatus { get; set; }\\n    }\\n\\n    public class ApiValidationFailure\\n    {\\n        public string Property { get; set; }\\n        public string FailureCode { get; set; }\\n        public string Details { get; set; }\\n    }\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nNow add a function that we'll use to wrap the call to Verify. The SMS containing our code will have the default message of \"Your verification code is 12345\" and will be from \"Example\". \n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"    SendCodeResponse SendCode(string phoneNumber)\\n    {\\n        var client = new RestClient(\\\"https://services.dynmark.com/webapi/verify\\\");\\n        client.Authenticator = new HttpBasicAuthenticator(username, password);\\n        client.Timeout = 130000;\\n\\n        var body = new\\n        {\\n            from = \\\"Example\\\",\\n            to = phoneNumber\\n        };\\n\\n        var request = new RestRequest();\\n        request.AddJsonBody(body);\\n\\n        var response = client.Post<SendCodeResponse>(request);\\n\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nWe should also check the response for success (202 Created) and failure due to user input error (400 Bad Request) and failure due to bad credentials (401 Unauthorised)\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"        if (response.StatusCode == HttpStatusCode.Created)\\n        {\\n            return response.Data;\\n        }\\n        else if (response.StatusCode == HttpStatusCode.Unauthorized)\\n        {\\n            throw new ArgumentException(\\\"Invalid credentials\\\");\\n        }\\n        else if (response.StatusCode == HttpStatusCode.BadRequest)\\n        {\\n            var failures = new JsonDeserializer().Deserialize<List<ApiValidationFailure>>(response);\\n\\n            if (failures.Any(f => f.FailureCode == \\\"Throttled\\\"))\\n            {\\n                throw new ArgumentException(\\\"Request to phone number too frequent\\\");\\n            }\\n            else if (failures.Any(f => f.FailureCode == \\\"ToInvalid\\\"))\\n            {\\n                throw new ArgumentException(\\\"Your phone number doesn't look like a valid number.\\\");\\n            }\\n            else\\n            {\\n                // Handle failure due to bad input\\n                throw new ArgumentException(\\\"There was an error in the request\\\");\\n            }\\n        }\\n\\n        throw new ArgumentException(\\\"Non success response\\\");\\n    }\\n}\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nNow let's add the HTML form and some processing to handle its postback\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<html>\\n<head></head>\\n<body>\\n    @{\\n        string phoneNumber = Request[\\\"phoneNumber\\\"];\\n        if (!string.IsNullOrEmpty(phoneNumber))\\n        {\\n            try\\n            {\\n                SendCodeResponse response = SendCode(phoneNumber);\\n\\n                if (response.NumberStatus == \\\"On\\\" || response.NumberStatus == \\\"Off\\\")\\n                {\\n                    Response.Redirect(\\\"Verify.cshtml?requestId=\\\" + response.RequestId);\\n                }\\n                else\\n                {\\n                    <div>Sorry, we couldn't sent you a verify code.</div>\\n                }\\n            }\\n            catch (ArgumentException ex)\\n            {\\n                <div>@ex.Message</div>\\n            }\\n            <p>\\n                Send another code? Try again below:\\n            </p>\\n        }\\n    }\\n\\n    <form action=\\\"Register.cshtml\\\" method=\\\"POST\\\">\\n        <p>To complete your registration, please enter your mobile phone number. A verification code will be sent to this number.</p>\\n        <input type=\\\"text\\\" name=\\\"phoneNumber\\\" value=\\\"447700090000\\\" />\\n        <input type=\\\"submit\\\" value=\\\"Register\\\" />\\n    </form>\\n</body>\\n</html>\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nWhen the postback occurs, we attempt to send a verification code to the supplied phone number. If this is successful we store the returned requestId and redirect the browser to the validation page for the user to enter the code they received. This page follows the same pattern: support classes and wrapper function\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"@using System\\n@using RestSharp\\n@using RestSharp.Authenticators\\n@using RestSharp.Deserializers\\n\\n@functions {\\n    // set our username and password\\n    string username = \\\"@Username@\\\";\\n    string password = \\\"@Password@\\\";\\n\\n    public class ValidateResponse\\n    {\\n        public string Status { get; set; }\\n    }\\n\\n    public class ApiValidationFailure\\n    {\\n        public string Property { get; set; }\\n        public string FailureCode { get; set; }\\n        public string Details { get; set; }\\n    }\\n\\n    ValidateResponse VerifyCode(Guid requestId, string code)\\n    {\\n        // instantiate a new HttpClient\\n        var client = new RestClient(string.Format(\\\"https://services.dynmark.com/webapi/verify/{0}/validate\\\", requestId));\\n        client.Authenticator = new HttpBasicAuthenticator(username, password);\\n        client.Timeout = 130000;\\n\\n        var body = new\\n        {\\n            code\\n        };\\n\\n        var request = new RestRequest();\\n        request.AddJsonBody(body);\\n\\n        var response = client.Put<ValidateResponse>(request);\\n\\n        if (response.StatusCode == HttpStatusCode.OK)\\n        {\\n            return response.Data;\\n        }\\n        if (response.StatusCode == HttpStatusCode.NoContent)\\n        {\\n            throw new ArgumentException(\\\"Your code was not recognised.\\\");\\n        }\\n        else if (response.StatusCode == HttpStatusCode.Unauthorized)\\n        {\\n            throw new ArgumentException(\\\"Invalid credentials\\\");\\n        }\\n        else if (response.StatusCode == HttpStatusCode.BadRequest)\\n        {\\n            var failures = new JsonDeserializer().Deserialize<List<ApiValidationFailure>>(response);\\n\\n            // Handle failure due to bad input\\n            throw new ArgumentException(\\\"There was an error in the request\\\");\\n        }\\n\\n        throw new ArgumentException(\\\"Non success response\\\");\\n    }\\n}\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\nNote that we can receive an HTTP 200 OK meaning that the request id or a 204 No Content. And finally HTML form and postback handler code\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<html>\\n<head>\\n</head>\\n<body>\\n    @{\\n        Guid requestId = (Guid)Session[\\\"requestId\\\"];\\n        string code = Request[\\\"Code\\\"];\\n        bool showCodeInput = true;\\n\\n        if (!string.IsNullOrEmpty(code))\\n        {\\n            try\\n            {\\n                var response = VerifyCode(requestId, code);\\n\\n                if (response.Status == \\\"CodeVerified\\\")\\n                {\\n                    showCodeInput = false;\\n                    <div>Your code was correct and is verified.</div>\\n                }\\n            }\\n            catch (ArgumentException ex)\\n            {\\n                <div>@ex.Message</div>\\n                <p>\\n                    Try again?\\n                </p>\\n\\t\\t\\t\\t\\t\\t}\\n        }\\n\\n        if (showCodeInput)\\n        {\\n            <form action=\\\"Verify.cshtml\\\" method=\\\"POST\\\">\\n                <p>Please enter the code we sent to complete your registration</p>\\n                <input type=\\\"text\\\" name=\\\"code\\\" />\\n                <input type=\\\"submit\\\" value=\\\"Verify\\\" />\\n            </form>\\n        }\\n    }\\n</body>\\n</html>\\n\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]","excerpt":"An example of how Verify can be used as part of an authentication process to check the owner of a mobile number","slug":"qs-cs-verify","type":"basic","title":"Use Verify to check mobile number ownership"}

Use Verify to check mobile number ownership

An example of how Verify can be used as part of an authentication process to check the owner of a mobile number

[block:callout] { "type": "info", "body": "Full source for these quickstarts can be downloaded from http://download.dynmark.com/Files/API/Quickstart/CS.zip" } [/block] ##Prerequisites You'll need a Dynmark account to be able to send messages. You can use a trial account to send a limited number of free messages for testing. ##Registration We're going to create a simple web application that sends a verification code via SMS when the user enters their phone number. We'll make use of the RestSharp library to handle the REST call to the Dynmark API. First we'll build the registration page to ask the user for their phone number. We'll start by defining the Dynmark username and password used to authenticate with the API, and some API response classes. [block:code] { "codes": [ { "code": "@using System\n@using System.Collections.Generic\n@using System.Linq\n@using RestSharp\n@using RestSharp.Authenticators\n@using RestSharp.Deserializers\n\n@functions {\n // set our Dynmark username and password\n string username = \"@Username@\";\n string password = \"@Password@\";\n \n public class SendCodeResponse\n {\n public Guid RequestId { get; set; }\n public string To { get; set; }\n public string NumberStatus { get; set; }\n }\n\n public class ApiValidationFailure\n {\n public string Property { get; set; }\n public string FailureCode { get; set; }\n public string Details { get; set; }\n }", "language": "csharp" } ] } [/block] Now add a function that we'll use to wrap the call to Verify. The SMS containing our code will have the default message of "Your verification code is 12345" and will be from "Example". [block:code] { "codes": [ { "code": " SendCodeResponse SendCode(string phoneNumber)\n {\n var client = new RestClient(\"https://services.dynmark.com/webapi/verify\");\n client.Authenticator = new HttpBasicAuthenticator(username, password);\n client.Timeout = 130000;\n\n var body = new\n {\n from = \"Example\",\n to = phoneNumber\n };\n\n var request = new RestRequest();\n request.AddJsonBody(body);\n\n var response = client.Post<SendCodeResponse>(request);\n", "language": "csharp" } ] } [/block] We should also check the response for success (202 Created) and failure due to user input error (400 Bad Request) and failure due to bad credentials (401 Unauthorised) [block:code] { "codes": [ { "code": " if (response.StatusCode == HttpStatusCode.Created)\n {\n return response.Data;\n }\n else if (response.StatusCode == HttpStatusCode.Unauthorized)\n {\n throw new ArgumentException(\"Invalid credentials\");\n }\n else if (response.StatusCode == HttpStatusCode.BadRequest)\n {\n var failures = new JsonDeserializer().Deserialize<List<ApiValidationFailure>>(response);\n\n if (failures.Any(f => f.FailureCode == \"Throttled\"))\n {\n throw new ArgumentException(\"Request to phone number too frequent\");\n }\n else if (failures.Any(f => f.FailureCode == \"ToInvalid\"))\n {\n throw new ArgumentException(\"Your phone number doesn't look like a valid number.\");\n }\n else\n {\n // Handle failure due to bad input\n throw new ArgumentException(\"There was an error in the request\");\n }\n }\n\n throw new ArgumentException(\"Non success response\");\n }\n}", "language": "csharp" } ] } [/block] Now let's add the HTML form and some processing to handle its postback [block:code] { "codes": [ { "code": "<html>\n<head></head>\n<body>\n @{\n string phoneNumber = Request[\"phoneNumber\"];\n if (!string.IsNullOrEmpty(phoneNumber))\n {\n try\n {\n SendCodeResponse response = SendCode(phoneNumber);\n\n if (response.NumberStatus == \"On\" || response.NumberStatus == \"Off\")\n {\n Response.Redirect(\"Verify.cshtml?requestId=\" + response.RequestId);\n }\n else\n {\n <div>Sorry, we couldn't sent you a verify code.</div>\n }\n }\n catch (ArgumentException ex)\n {\n <div>@ex.Message</div>\n }\n <p>\n Send another code? Try again below:\n </p>\n }\n }\n\n <form action=\"Register.cshtml\" method=\"POST\">\n <p>To complete your registration, please enter your mobile phone number. A verification code will be sent to this number.</p>\n <input type=\"text\" name=\"phoneNumber\" value=\"447700090000\" />\n <input type=\"submit\" value=\"Register\" />\n </form>\n</body>\n</html>", "language": "csharp" } ] } [/block] When the postback occurs, we attempt to send a verification code to the supplied phone number. If this is successful we store the returned requestId and redirect the browser to the validation page for the user to enter the code they received. This page follows the same pattern: support classes and wrapper function [block:code] { "codes": [ { "code": "@using System\n@using RestSharp\n@using RestSharp.Authenticators\n@using RestSharp.Deserializers\n\n@functions {\n // set our username and password\n string username = \"@Username@\";\n string password = \"@Password@\";\n\n public class ValidateResponse\n {\n public string Status { get; set; }\n }\n\n public class ApiValidationFailure\n {\n public string Property { get; set; }\n public string FailureCode { get; set; }\n public string Details { get; set; }\n }\n\n ValidateResponse VerifyCode(Guid requestId, string code)\n {\n // instantiate a new HttpClient\n var client = new RestClient(string.Format(\"https://services.dynmark.com/webapi/verify/{0}/validate\", requestId));\n client.Authenticator = new HttpBasicAuthenticator(username, password);\n client.Timeout = 130000;\n\n var body = new\n {\n code\n };\n\n var request = new RestRequest();\n request.AddJsonBody(body);\n\n var response = client.Put<ValidateResponse>(request);\n\n if (response.StatusCode == HttpStatusCode.OK)\n {\n return response.Data;\n }\n if (response.StatusCode == HttpStatusCode.NoContent)\n {\n throw new ArgumentException(\"Your code was not recognised.\");\n }\n else if (response.StatusCode == HttpStatusCode.Unauthorized)\n {\n throw new ArgumentException(\"Invalid credentials\");\n }\n else if (response.StatusCode == HttpStatusCode.BadRequest)\n {\n var failures = new JsonDeserializer().Deserialize<List<ApiValidationFailure>>(response);\n\n // Handle failure due to bad input\n throw new ArgumentException(\"There was an error in the request\");\n }\n\n throw new ArgumentException(\"Non success response\");\n }\n}", "language": "csharp" } ] } [/block] Note that we can receive an HTTP 200 OK meaning that the request id or a 204 No Content. And finally HTML form and postback handler code [block:code] { "codes": [ { "code": "<html>\n<head>\n</head>\n<body>\n @{\n Guid requestId = (Guid)Session[\"requestId\"];\n string code = Request[\"Code\"];\n bool showCodeInput = true;\n\n if (!string.IsNullOrEmpty(code))\n {\n try\n {\n var response = VerifyCode(requestId, code);\n\n if (response.Status == \"CodeVerified\")\n {\n showCodeInput = false;\n <div>Your code was correct and is verified.</div>\n }\n }\n catch (ArgumentException ex)\n {\n <div>@ex.Message</div>\n <p>\n Try again?\n </p>\n\t\t\t\t\t\t}\n }\n\n if (showCodeInput)\n {\n <form action=\"Verify.cshtml\" method=\"POST\">\n <p>Please enter the code we sent to complete your registration</p>\n <input type=\"text\" name=\"code\" />\n <input type=\"submit\" value=\"Verify\" />\n </form>\n }\n }\n</body>\n</html>\n", "language": "csharp" } ] } [/block]