{"id":293,"date":"2014-06-22T22:18:00","date_gmt":"2014-06-22T22:18:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/odatateam\/2014\/06\/22\/tutorial-sample-how-to-enable-authentication-for-metadata-documents-in-the-odata-client-code-generator\/"},"modified":"2024-02-12T13:58:12","modified_gmt":"2024-02-12T20:58:12","slug":"tutorial-sample-how-to-enable-authentication-for-metadata-documents-in-the-odata-client-code-generator","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/odata\/tutorial-sample-how-to-enable-authentication-for-metadata-documents-in-the-odata-client-code-generator\/","title":{"rendered":"[Tutorial &amp; Sample] How to enable authentication for metadata documents in the OData Client Code Generator"},"content":{"rendered":"<p>Currently, the OData Client Code Generator doesn\u2019t support authentication for metadata documents. Although authenticating the access to OData metadata documents is not encouraged for service builders unless they has a good reason to do so, However, it might not be easy to exclude $metadata from the authentication required by the service.<\/p>\n<p>So in this tutorial, I will give a sample on how to update the OData Client Code Generator to customize the metadata documents authentication support.<\/p>\n<h3>Update the Code Generator Files<ins cite=\"mailto:Layla%20Liu\" datetime=\"2014-06-10T15:15\"> <\/ins><\/h3>\n<p>You can follow \u201c<a href=\"http:\/\/blogs.msdn.com\/b\/odatateam\/archive\/2014\/03\/11\/how-to-use-odata-client-code-generator-to-generate-client-side-proxy-class.aspx\">How to use OData client code generator to generate client side proxy class<\/a>\u201d to add OData client proxy file into your project. It will add two files. One is \u201c*.odata.config\u201d, another is \u201c*.tt\u201d. (I will use \u201cODataT4Template.odata.config\u201d and \u201cODataT4Template.tt\u201d in this doc). Now, you can begin to implement the metadata documents authentication support.<ins cite=\"mailto:Layla%20Liu\" datetime=\"2014-06-10T15:27\"> <\/ins><\/p>\n<p>In this Tutorial, I will take the basic username-password authentication as an example.<\/p>\n<h4>Define authentication information in ODataT4Template.odata.config<\/h4>\n<p>&#8220;ODataT4Template.odata.config&#8221; is a file to collect all input information. Users can add following codes into &#8220;ODataT4Template.odata.config&#8221;.<\/p>\n<p><script type=\"text\/javascript\" src=\"https:\/\/gist.github.com\/ODataTeam\/0f00ca83f7efccd87def.js\"><\/script><\/p>\n<p>Every time a user generates client code for a service which needs authentication, the user should set this information.<\/p>\n<h4>Apply authentication logic in \u201cODataT4Template.tt\u201d<\/h4>\n<p>To support the basic username-password authentication, we plan to use <a>&#8220;<\/a><strong>HttpWebRequest.Credentials<\/strong><a>&#8220;<\/a> to set authentication information for requests. So in ODataT4Template.tt file, we add a <a>&#8220;<\/a>NetworkCredential<a>&#8220;<\/a> property into the OData Code Generator.<\/p>\n<p>Please add following code\u00a0to the highlighted place<\/p>\n<p><script type=\"text\/javascript\" src=\"https:\/\/gist.github.com\/ODataTeam\/2a0249fcd04afecc796a.js\"><\/script><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2014\/06\/7573.Config1.jpg\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2014\/06\/7573.Config1.jpg\" alt=\"\" border=\"0\" \/><\/a><\/p>\n<p>We also need to add a logic to parse the credential information from <a>&#8220;<\/a>ODataT4Template.odata.config<a>&#8220;<\/a> into the property. Since the existing ODataT4Template.tt file has contained a method <a>&#8220;<\/a><strong>ApplyParametersFromConfigFile<\/strong><a>&#8220;<\/a> to parse the configuration file. So we only need to add the logic for authentication information.<\/p>\n<p>New the networkCreditial, and set all the properties.<\/p>\n<p><script type=\"text\/javascript\" src=\"https:\/\/gist.github.com\/ODataTeam\/f6e678df245d3fdbd024.js\"><\/script><\/p>\n<p>Then, we need a way to send those authentication information along with http requests to the server. Here, we define a <a>&#8220;<\/a>Customization.CustomizeHttpRequestAuthentication<a>&#8220;<\/a> method to set the credential for requests.<\/p>\n<p>Add following code to the highlighted place<\/p>\n<p><script type=\"text\/javascript\" src=\"https:\/\/gist.github.com\/ODataTeam\/7f8f11eeda98385f19f3.js\"><\/script><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2014\/06\/1538.Customize.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2014\/06\/1538.Customize.png\" alt=\"\" border=\"0\" \/><\/a><\/p>\n<p>Call the customized method before sending requests by &#8220;Customization.CustomizeHttpRequestAuthentication(webRequest, Credential)&#8221;<\/p>\n<p><script type=\"text\/javascript\" src=\"https:\/\/gist.github.com\/ODataTeam\/4008631410b06b753c7e.js\"><\/script><\/p>\n<p>Now you have enabled the basic username-password authentication for accessing OData metadata documents.<\/p>\n<p>In conclusion, the main flow is as following.<\/p>\n<p>1. Add configuration items in <a>&#8220;<\/a>ODataT4Template.odata.config<a>&#8220;<\/a> to set authentication information.<\/p>\n<p>2. In the code generator, define a property to store the authenticate information.<\/p>\n<p>3. Add logic to parse those information from the configuration file to the property.<\/p>\n<p>4. Define a method to add authentication information into requests. And call it before you send the requests.<\/p>\n<p>Users can follow this flow to customize their own way to authenticate access to $metadata. <ins cite=\"mailto:Layla%20Liu\" datetime=\"2014-06-10T15:15\"><\/ins><\/p>\n<h3>Prepare a VSIX installation Package<\/h3>\n<p>If you want to contribute these changes to your team, then you can apply your changes into the ODataT4Template.vsix. Then your team members just need to install the vsix to enable authentication support.<\/p>\n<h4>Download the VSIX Package<\/h4>\n<p>Access the <a>&#8220;<\/a><a href=\"https:\/\/visualstudiogallery.msdn.microsoft.com\/9b786c0e-79d1-4a50-89a5-125e57475937\">OData Client Code Generator<\/a><a>&#8220;<\/a> page. Click <a>&#8220;<\/a><strong>Download<\/strong><a>&#8220;<\/a>. Save the VSIX package to the local machine. (Example: C:VSIX)<\/p>\n<h4>Unpackage the VSIX Package<\/h4>\n<p>1. In the folder <a>&#8220;<\/a><em>C:VSIX<a>&#8220;<\/a><\/em>, unzip <a>&#8220;<\/a><strong>ODataT4ItemTemplate.1.0.0.vsix<\/strong><a>&#8220;<\/a> to <a>&#8220;<\/a><strong>ODataT4ItemTemplate.1.0.0<\/strong><a>&#8220;<\/a>.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2014\/06\/7774.unzip_.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2014\/06\/7774.unzip_.png\" alt=\"\" border=\"0\" \/><\/a><\/p>\n<p>2. Open the folder <em>\u201cC:VSIXODataT4ItemTemplate.1.0.0T4CSharpCode\u201d.<\/em><\/p>\n<p>3. Unzip \u201c<strong>ODataT4TemplateCSharp.1.0.0.zip<\/strong>\u201d to \u201c<strong>ODataT4TemplateCSharp.1.0.0<\/strong>\u201d<\/p>\n<p>Open the \u201cODataT4TemplateCSharp.1.0.0\u201d folder. You can update \u201c<strong>ODataT4Template.tt\u201d<\/strong> and \u201c<strong>ODataT4Template.odata.config\u201d<\/strong> to customize authentication.<\/p>\n<p><strong>Note<\/strong><\/p>\n<p>If you are using VB on client, you can find that \u201cODataT4Template.tt\u201d and \u201cODataT4Template.odata.config\u201d for VB are packaged in \u201cODataT4TemplateVisualBasic.1.0.0.zip\u201d under <em>\u201cC:VSIXODataT4ItemTemplate.1.0.0T4VisualBasicCode\u201d<\/em>. Then you can take this tutorial as reference to update those files.<\/p>\n<h4>Update Code Generator Files<\/h4>\n<p>Refer to &#8220;Update the Code Generator Files&#8221; section above.<\/p>\n<h4>Repack the Updated Code Generator Files<\/h4>\n<p>After all files have been updated, you should pack them back to the original package. Make sure the structure of the zip file is same as before.<\/p>\n<p>1. In folder <a>&#8220;<\/a><em>C:VSIXODataT4ItemTemplate.1.0.0T4CSharpCodeODataT4TemplateCSharp.1.0.0<\/em><a>&#8220;<\/a>, package all files into\u00a0<ins cite=\"mailto:Yi%20Ding\" datetime=\"2014-06-10T13:07\"><a>&#8220;<\/a><\/ins><strong>ODataT4TemplateCSharp.1.0.0.zip<\/strong>&#8220;.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2014\/06\/6102.zip1_.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2014\/06\/6102.zip1_.png\" alt=\"\" border=\"0\" \/><\/a><\/p>\n<p>2. Use this file to replace the same named file in folder <a>&#8220;<\/a><em>C:VSIXODataT4ItemTemplate.1.0.0T4CSharpCode<\/em><a>&#8220;<\/a>.<\/p>\n<p>3. Delete the folder <a>&#8220;<\/a>ODataT4TemplateCSharp.1.0.0<a>&#8220;<\/a>.<\/p>\n<p>4. In folder <a>&#8220;<\/a>C:VSIXODataT4ItemTemplate.1.0.0<a>&#8220;<\/a>, package all files into <a>&#8220;<\/a><strong>ODataT4ItemTemplate.1.0.0.zip<\/strong><a>&#8220;<\/a><\/p>\n<p>5. Rename <a>&#8220;<\/a><strong>ODataT4ItemTemplate.1.0.0.zip<\/strong><a>&#8220;<\/a>\u00a0to <a>&#8220;<\/a><strong>ODataT4ItemTemplate.1.0.0.vsix<\/strong><a>&#8220;<\/a><\/p>\n<p>Then, you can uninstall the old ODataT4ItemTemplate.1.0.0.vsix, and reinstall this VSIX package. You will find that the basic username-password authentication has been enabled for accessing OData metadata documents.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Currently, the OData Client Code Generator doesn\u2019t support authentication for metadata documents. Although authenticating the access to OData metadata documents is not encouraged for service builders unless they has a good reason to do so, However, it might not be easy to exclude $metadata from the authentication required by the service. So in this tutorial, [&hellip;]<\/p>\n","protected":false},"author":518,"featured_media":3253,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[50],"class_list":["post-293","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-odata","tag-odata-client-tools"],"acf":[],"blog_post_summary":"<p>Currently, the OData Client Code Generator doesn\u2019t support authentication for metadata documents. Although authenticating the access to OData metadata documents is not encouraged for service builders unless they has a good reason to do so, However, it might not be easy to exclude $metadata from the authentication required by the service. So in this tutorial, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/posts\/293","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/users\/518"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/comments?post=293"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/posts\/293\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/media\/3253"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/media?parent=293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/categories?post=293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/tags?post=293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}