Windows 10 App :Host Defined Policy: Inline Script. Resource will be blocked.

I recently ported one of my java script projects to Windows store app and got the error as following

CSP14321: Resource violated directive ‘script-src ms-appx: ‘unsafe-eval’ blob:’ in Host Defined Policy: inline script, in ms-appx://13bdc914-5111-4c95-a5e6-82029c5105ec/index.html at line 21 column 12. Resource will be blocked.

If you have something like the following, it won’t work

<a href="javascript:void(0)" id="somelink" onclick="callMethod();">Link</a>

Since I was using jQuery in the project, I simply used jQuery to bind click event to the element

$("#somelink").click(function () {
    callMethod();
});

and Voila, everything works but I had many pages which had this reference and I had to make this change to all pages. After some research I found that there is an alternate way which is much faster to change. By default ms-appx:/// protocol is used in store apps to fetch the content. For web apps specify a specific protocal ms-appx-web which is then used for the app. This can be done by changing start page from index.html to ms-appx-web:///index.html in package.appxmanifest. Also add highlighted rule in your package.appxmanifest (You will need to modify it in coding view and not the visual editor)

<?xml version="1.0" encoding="utf-8"?>
<Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10" xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest" xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" IgnorableNamespaces="uap mp">
  <Identity Name="1xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" Version="1.0.0.0" Publisher="CN=User" />
  <mp:PhoneIdentity PhoneProductId="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" PhonePublisherId="00000000-0000-0000-0000-000000000000" />
  <Properties>
    <DisplayName>Application</DisplayName>
    <PublisherDisplayName>User</PublisherDisplayName>
    <Logo>images\storelogo.png</Logo>
  </Properties>
  <Dependencies>
    <TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.0.0" MaxVersionTested="10.0.0.0" />
  </Dependencies>
  <Resources>
    <Resource Language="x-generate" />
  </Resources>
  <Applications>
    <Application Id="App" StartPage="ms-appx-web:///index.html">
      <uap:VisualElements DisplayName="MyApp" Description="My App Description" BackgroundColor="transparent" Square150x150Logo="images\Square150x150Logo.png" Square44x44Logo="images\Square44x44Logo.png">
        <uap:DefaultTile Wide310x150Logo="images\Wide310x150Logo.png">
        </uap:DefaultTile>
        <uap:SplashScreen Image="images\splashscreen.png" />
      </uap:VisualElements>
      <uap:ApplicationContentUriRules>
        <uap:Rule Match="ms-appx-web:///" Type="include" WindowsRuntimeAccess="all" />
      </uap:ApplicationContentUriRules>
    </Application>
  </Applications>
  <Capabilities>
    <Capability Name="internetClient" />
  </Capabilities>
</Package>

Quick and easy..

 


Leave A Comment

Your email address will not be published.