マイナーでもいいよね??

殆どVB系、でも .NET じゃない VB は知らないよん

目次

Blog 利用状況

書庫

日記カテゴリ

ドメインコントローラのIPアドレスの取得

前々回、取得した Domain オブジェクトの表示で、ドメインコントローラのIPアドレスについては Web アプリの場合、ローカルホストだと「::1」と表示されてしまうことがある と書きました。

Windows アプリの場合は、ListBox で選択したドメインコントローラ(DomainController クラス)の IPAddress プロパティの値をそのまま表示してますが、Web アプリの場合は次のメソッドからドメインコントローラのIPアドレスを取得するようにしました。

※名前空間 System.Net と System.Net.Sockets をインポートしてます。

 

VB

Private Function GetDomainControllerIPAddress(dc As DomainController) As String

  If dc.IPAddress.Contains(".") Then  'IPv4の時

    Return dc.IPAddress

  End If

 

  Dim ip = IPAddress.Parse(dc.IPAddress)

  If IPAddress.IsLoopback(ip) Then  'ループバックの時

    Dim addresses = Dns.GetHostAddresses(String.Empty)  'ローカルホストのIPアドレス

    ip = addresses.FirstOrDefault(Function(address) address.AddressFamily = AddressFamily.InterNetwork)   'IPv4のアドレス

    If ip Is Nothing Then   'IPv4のアドレスがない時

      ip = addresses(0)   '先頭のIPv6のアドレス

    End If

    Return ip.ToString()

  End If

  Return dc.IPAddress

End Function

 

C#

private string GetDomainControllerIPAddress(DomainController dc)

{

  if (dc.IPAddress.Contains("."))   //IPv4の時

  {

    return dc.IPAddress;

  }

 

  var ip = IPAddress.Parse(dc.IPAddress);

  if (IPAddress.IsLoopback(ip))   //ループバックの時

  {

    var addresses = Dns.GetHostAddresses(String.Empty);   //ローカルホストのIPアドレス

    ip = addresses.FirstOrDefault(address => address.AddressFamily == AddressFamily.InterNetwork);  //IPv4のアドレス

    if (ip == null)   //IPv4のアドレスがない時

    {

      ip = addresses[0];  //先頭のIPv6のアドレス

    }

    return ip.ToString();

  }

  return dc.IPAddress;

}

 

IPv4 を優先してます。ループバックの時は DNS サーバから IPアドレスを取得してます。

投稿日時 : 2013年6月23日 1:14

コメントを追加

# Good day very cool web site!! Man .. Excellent .. Superb .. I'll bookmark your web site and take the feeds additionally? I'm glad to seek out a lot of helpful information right here in the submit, we want work out more techniques on this regard, thanks f 2019/05/11 4:56 Good day very cool web site!! Man .. Excellent ..

Good day very cool web site!! Man .. Excellent ..
Superb .. I'll bookmark your web site and take the feeds additionally?
I'm glad to seek out a lot of helpful information right
here in the submit, we want work out more techniques on this regard, thanks for sharing.
. . . . .

# Hello there! I know this is kind of off topic but I was wondering which blog platform are you using for this website? I'm getting tired of Wordpress because I've had issues with hackers and I'm looking at alternatives for another platform. I would be aw 2019/08/19 2:36 Hello there! I know this is kind of off topic but

Hello there! I know this is kind of off topic but I was wondering which blog
platform are you using for this website? I'm getting tired of Wordpress because I've had issues with
hackers and I'm looking at alternatives for another platform.
I would be awesome if you could point me in the direction of a good platform.

# Hello there! I know this is kind of off topic but I was wondering which blog platform are you using for this website? I'm getting tired of Wordpress because I've had issues with hackers and I'm looking at alternatives for another platform. I would be aw 2019/08/19 2:37 Hello there! I know this is kind of off topic but

Hello there! I know this is kind of off topic but I was wondering which blog
platform are you using for this website? I'm getting tired of Wordpress because I've had issues with
hackers and I'm looking at alternatives for another platform.
I would be awesome if you could point me in the direction of a good platform.

# Hello there! I know this is kind of off topic but I was wondering which blog platform are you using for this website? I'm getting tired of Wordpress because I've had issues with hackers and I'm looking at alternatives for another platform. I would be aw 2019/08/19 2:38 Hello there! I know this is kind of off topic but

Hello there! I know this is kind of off topic but I was wondering which blog
platform are you using for this website? I'm getting tired of Wordpress because I've had issues with
hackers and I'm looking at alternatives for another platform.
I would be awesome if you could point me in the direction of a good platform.

# Hello there! I know this is kind of off topic but I was wondering which blog platform are you using for this website? I'm getting tired of Wordpress because I've had issues with hackers and I'm looking at alternatives for another platform. I would be aw 2019/08/19 2:39 Hello there! I know this is kind of off topic but

Hello there! I know this is kind of off topic but I was wondering which blog
platform are you using for this website? I'm getting tired of Wordpress because I've had issues with
hackers and I'm looking at alternatives for another platform.
I would be awesome if you could point me in the direction of a good platform.

# Heya i am for the first time here. I came across this board and I find It really useful & it helped me out a lot. I hope to give something back and aid others like you aided me. 2019/08/24 10:43 Heya i am for the first time here. I came across t

Heya i am for the first time here. I came across this board
and I find It really useful & it helped me out a lot.
I hope to give something back and aid others
like you aided me.

# My brother suggested I might like this web site. He was totally right. This post actually made my day. You cann't imagine just how much time I had spent for this info! Thanks! 2019/09/08 3:18 My brother suggested I might like this web site. H

My brother suggested I might like this web site. He was totally right.
This post actually made my day. You cann't imagine just how much time I had spent for this info!
Thanks!

# KimYmxNKnwcETeXPO 2022/04/19 11:44 johnansaz

http://imrdsoacha.gov.co/silvitra-120mg-qrms

タイトル
名前
URL
コメント