0

I have tried to get host name from OWIN Startup class by using below code in ASP.NET web forms. I am getting http://127.0.0.1 host name instead of http://localhsot:portnumber. Also I am not able to get real host name i.e., www.testsite.com. Is there any options available to get actual host name from OWIN Startup class?

[assembly: OwinStartup(typeof(Startup))]        
public class Startup
{
  public void Configuration(IAppBuilder app)
  {
     app.UseCookieAuthentication(new CookieAuthenticationOptions
     {
       AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
       ExpireTimeSpan = TimeSpan.FromMinutes(10),
       SlidingExpiration = true
     });
     app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
     var host = HttpContext.Current.Request.Url.Host.ToString().ToLower(); <-- I am getting http://127.0.0.1 instead of http://localhsot:portnumber.         
     var queryString = "";
     if (host.Contains("technicolor"))
     {
       queryString = "1";
     }
     else if (host.Contains("3") || HttpContext.Current.Request.IsLocal)
     {
       queryString = "2";
     }
     else {
       queryString = "3"
     }

     var options = new PingAuthenticationOptions(
                       pingAuthenticationUrl: ConfigurationManager.AppSettings["Ping::LoginUrl"] + ACSIdx,
                       pingLogoutUrl: ConfigurationManager.AppSettings["Ping::LogoutUrl"],
                       certificateFile: certificate,
                       pingResponseDestination: ConfigurationManager.AppSettings["Ping::DestinationUrl"],
                       externalLoginCallback: ConfigurationManager.AppSettings["Ping::ExternalLoginCallback"],
                       samlProcessor: new SamlProcessor());
        app.UsePingAuthentication(options);
  }
}
RGS
  • 5,131
  • 6
  • 37
  • 65
  • probably because you contacted it as localhost, which is just that rather than its local IP of say 192.168.0.1 or whatever.. – BugFinder Dec 02 '19 at 09:13
  • @BugFinder, If I hosted in IIS, will I get actual host name for example http://www.testsite.com? – RGS Dec 02 '19 at 09:44
  • I would expect so. – BugFinder Dec 02 '19 at 09:47
  • @BugFinder, I am unable to find host name using above code in IIS. – RGS Dec 02 '19 at 17:00
  • You cannot get the host name during Owin startup; you have to wait until the first request has been received. –  Dec 02 '19 at 18:49
  • @Amy, I have updated my question. I have to append the querystring value in PingAuthenticationOptions dynamically. Is there any alternate method available to get host name? – RGS Dec 02 '19 at 19:07
  • @Anders Abel, If you have spare time, please has a look on this question. – RGS Dec 02 '19 at 23:28
  • As @Amy said there is no way to access to request before first request.If you would like to access to some property of your hosting you can use HostingEnvironment information( for example SiteName). I suggest you define a middleware to add your query string to Request.QueryString. – Pbehin Dec 04 '19 at 05:55

0 Answers0