0

I am attempting to have seamless movement between locally testing and cloud testing of my Azure Web App. The following is a snippet from my Web.Config file.

<appSettings>
    <add key="ida:ClientId" value="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" />
    ... more items ...
</appSettings>

The value of ClientId corresponds to the value of an "App Registrations" object within my Azure Active Directory instance. When I click on that object and navigate to Settings --> Reply URLs, the entry is https://localhost:xxxxx/.

My problem is: When I publish the Azure Web App to the cloud, I am redirected to the above localhost URL. However, if I add another Reply URL entry of https://mywebappname.mydomain.com, then when I attempt to test locally, I am redirected to the web URL instead of the localhost URL.

I would like a method of seamless local and cloud testing: When testing locally and on the cloud, I would like to be redirected to the correct URL. What configurations am I missing in order to accomplish this?

---- UPDATE ----

Also when I right click on the Web Application project in Visual Studio 2017 and then click Properties, I am taken to this screen. Here are some additional properties I have set, which redirect to https://localhost:xxxxx/. Do I need to change this? And if so, how can I have both redirect URLs within the project's properties?

Web App Properties

aBlaze
  • 2,436
  • 2
  • 31
  • 63
  • Use web.config transformations - [Web.config Transformation Syntax for Web Project Deployment Using Visual Studio](https://msdn.microsoft.com/en-us/library/dd465326(VS.100).aspx) –  Mar 03 '18 at 06:07
  • 1
    The problem is your app is not specifying the redirect URL. So AAD picks one at random. You gotta configure the authentication pieces so that they specify `redirect_uri`. – juunas Mar 03 '18 at 08:58
  • @juunas, can you please elaborate? Where in the app do I specify the redirect URL - maybe the Web.config? And do I have to specify multiple redirect URLs in the app? And if so, how will the app know which one to choose? – aBlaze Mar 03 '18 at 16:02
  • @StephenMuecke Thank you for the article about Transforms. However, I notice that the article is dated from May 2011, which is a long time ago. Is there an updated way of accomplishing this task, or is this the latest method? – aBlaze Mar 03 '18 at 16:03
  • @juunas I also added an updated containing a screenshot to help you see more of the configuration of the project. Thanks so much! – aBlaze Mar 03 '18 at 16:13
  • @Emilia. Its still applicable (and I suspect there are more recent articles but I will leave you to google that). By default VS creates [debug and release](https://stackoverflow.com/questions/2791236/what-are-the-web-debug-config-and-web-release-config-files-for) versions of the web.config file, but you can create more for publishing to different servers etc. –  Mar 03 '18 at 22:58
  • @Emilia Are basing your site on a sample? The config file you mention doesn't do any magic by itself--there's code somewhere using it. If you can share the relevant parts of the code (or point us to the sample you're basing this on) we can perhaps help you indicate where you would be able to choose the reply URL. – Philippe Signoret Mar 04 '18 at 00:39

1 Answers1

2

I ended up solving this problem by using the Web.config Transformation Syntax (see Stephen Muecke's link in the comments on the original post).

In the same directory as the Web.config, I added a Web.Release.config. Now, if in Visual Studio I select "Release" build, then the Web.Release.config transform config file will be used to replace the Web.config file's "ida:RedirectUri" key with the value in the transform config file.

Here is the Web.Release.config

<?xml version="1.0"?>
<!-- For more information on using Web.config transformation visit https://go.microsoft.com/fwlink/?LinkId=301874 -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <appSettings>
    <add key="ida:RedirectUri" value="https://<mywebappname>.<mywebappdomain>.com/" xdt:Transform="Replace" />
  </appSettings>
</configuration>

And here a snippet from the very large Web.config

<?xml version="1.0"?>
<!-- For more information on using Web.config transformation visit https://go.microsoft.com/fwlink/?LinkId=301874 -->
...
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  ...
  <appSettings>
    <add key="ida:RedirectUri" value="https://localhost:xxxxx/" xdt:Transform="Replace" />
    ...
  </appSettings>
  ...
</configuration>

That way, redirecting to localhost is the default.

aBlaze
  • 2,436
  • 2
  • 31
  • 63
  • When I try to run this I get an error that says unrecognized attribute 'xmlns:xdt'. Note that attribute names are case-sensitive – smuldr May 13 '19 at 15:18