성태의 닷넷 이야기
홈 주인
모아 놓은 자료
프로그래밍
질문/답변
사용자 관리
사용자
메뉴
아티클
외부 아티클
유용한 코드
온라인 기능
MathJax 입력기
최근 덧글
[정성태] Java - How to use the Foreign Funct...
[정성태] 제가 큰 실수를 했군요. ^^; Delegate를 통한 Bein...
[정성태] Working with Rust Libraries from C#...
[정성태] Detecting blocking calls using asyn...
[정성태] 아쉽게도, 커뮤니티는 아니고 개인 블로그입니다. ^^
[정성태] 질문이 잘 이해가 안 됩니다. 우선, 해당 소스코드에서 ILis...
[양승조
] var대신 dinamic으로 선언해서 해결은 했습니다. 맞는 해...
[양승조
] 또 막혔습니다. ㅠㅠ var list = props[i].Ge...
[양승조
] 아. 감사합니다. 어제는 안됐던것 같은데....정신을 차려야겠네...
[정성태] "props[i].GetValue(props[i])" 코드에서 ...
글쓰기
제목
이름
암호
전자우편
HTML
홈페이지
유형
제니퍼 .NET
닷넷
COM 개체 관련
스크립트
VC++
VS.NET IDE
Windows
Team Foundation Server
디버깅 기술
오류 유형
개발 환경 구성
웹
기타
Linux
Java
DDK
Math
Phone
Graphics
사물인터넷
부모글 보이기/감추기
내용
<div style='display: inline'> <br /> <div style='font-family: 맑은 고딕, Consolas; font-size: 20pt; color: #006699; text-align: center; font-weight: bold'>WCF - webHttpBinding 윈도우 인증 구현 예제</div><br /> <br /> 지난번 이야기에 이어서.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; width: 800px; background-color: #fbedbb; overflow-x: scroll; font-family: Consolas, Verdana;' > WCF - webHttpBinding 구현 예제 ; <a target='_tab' href='/2/0/833'>http://www.sysnet.pe.kr/2/0/833</a> </pre> <br /> 질문하신 분의 상황에 맞게 인증을 넣어보았습니다. 이전 소스 코드에서 사용한 바인딩이 다음과 같은데,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; width: 800px; background-color: #fbedbb; overflow-x: scroll; font-family: Consolas, Verdana;' > using (ServiceHost serviceHost = new ServiceHost(typeof(CHelloWorld), new Uri(baseAddress))) { ServiceEndpoint endpoint = serviceHost.AddServiceEndpoint(typeof(IHelloWorld), <b style='COLOR: blue'>new WebHttpBinding()</b>, ""); endpoint.Behaviors.Add(new WebHttpBehavior()); serviceHost.Open(); ...[생략]... } </pre> <br /> 여기에 인증을 걸기 위해 다음과 같이 변경합니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; width: 800px; background-color: #fbedbb; overflow-x: scroll; font-family: Consolas, Verdana;' > WebHttpBinding binding = new WebHttpBinding(); <b style='COLOR: blue'>binding.Security.Mode = WebHttpSecurityMode.TransportCredentialOnly; binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;</b> ServiceEndpoint endpoint = serviceHost.AddServiceEndpoint(typeof(IHelloWorld), binding, ""); </pre> <br /> 이제 WCF 서비스를 실행해 두고, 웹 브라우저로 접근하면 다음과 같이 인증 창을 묻게 됩니다.<br /> <br /> [그림 1: 윈도우 인증]<br /> <img alt='wcf_webhttpbinding_windows_credential_1.png' src='/SysWebRes/bbs/wcf_webhttpbinding_windows_credential_1.png' /><br /> <br /> 물론, 정상적인 인증 정보를 입력하면 해당 웹 메서드가 실행되어 반환값을 보내줍니다.<br /> <br /> <hr style='width: 50%' /><br /> <br /> WCF 클라이언트로도 확인해 볼까요? 같은 PC에서 테스트 하거나 또는 다른 PC인 경우라도 계정/암호가 동일하게 로그인했다면 다음과 같이 코드를 구성할 수 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; width: 800px; background-color: #fbedbb; overflow-x: scroll; font-family: Consolas, Verdana;' > WebHttpBinding clntBinding = new WebHttpBinding(); <b style='COLOR: blue'>clntBinding.Security.Mode = WebHttpSecurityMode.TransportCredentialOnly; clntBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;</b> using (ChannelFactory<IHelloWorld> factory = new ChannelFactory<IHelloWorld>(clntBinding, new EndpointAddress(baseAddress))) { factory.Endpoint.Behaviors.Add(new WebHttpBehavior()); IHelloWorld proxy = factory.CreateChannel(); Console.WriteLine("Result of Get operation: {0}", proxy.GetDate("1990", "05", "01")); } </pre> <br /> 만약, 명시적으로 다른 계정이라면 다음과 같이 반드시 계정 정보를 주어야 합니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; width: 800px; background-color: #fbedbb; overflow-x: scroll; font-family: Consolas, Verdana;' > using (ChannelFactory<IHelloWorld> factory = new ChannelFactory<IHelloWorld>(clntBinding, new EndpointAddress(baseAddress))) { <b style='COLOR: blue'>factory.Credentials.Windows.ClientCredential = new NetworkCredential("[계정]", "[암호]");</b> factory.Endpoint.Behaviors.Add(new WebHttpBehavior()); IHelloWorld proxy = factory.CreateChannel(); Console.WriteLine("Result of Get operation: {0}", proxy.GetDate("1990", "05", "01")); } </pre> <br /> HttpWebRequest로도 해볼까요? 역시 같은 PC이거나, 다른 컴퓨터에서 로그인 정보가 같은 경우에는 다음과 같이 코딩을 하고,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; width: 800px; background-color: #fbedbb; overflow-x: scroll; font-family: Consolas, Verdana;' > static void Main(string[] args) { string baseAddress = "http://192.168.0.55:9091/HelloService"; string reqAddress = string.Format("{0}/date/1990/05/05", baseAddress); HttpWebRequest req = WebRequest.Create(reqAddress) as HttpWebRequest; <b style='COLOR: blue'>req.UseDefaultCredentials = true;</b> WebResponse response = req.GetResponse(); Stream stream = response.GetResponseStream(); StreamReader sr = new StreamReader(stream); string text = sr.ReadToEnd(); Console.WriteLine(text); } </pre> <br /> 만약, 명시적으로 계정을 지정해야 한다면 WCF와 유사하게 다음과 같이 마무리 지을 수 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; width: 800px; background-color: #fbedbb; overflow-x: scroll; font-family: Consolas, Verdana;' > HttpWebRequest req = WebRequest.Create(reqAddress) as HttpWebRequest; //req.UseDefaultCredentials = true; req.Credentials = new NetworkCredential("계정", "암호"); </pre> <br /> <a target='_tab' href='http://www.sysnet.pe.kr/bbs/DownloadAttachment.aspx?fid=518&boardid=331301885'>첨부한 파일은 테스트가 바로 가능한 프로젝트 소스 코드</a>를 포함하고 있습니다. <br /> <br /> <br /> <br /> <br /><br /><hr /><span style='color: Maroon'>[이 토픽에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]</span> </div>
첨부파일
스팸 방지용 인증 번호
1564
(왼쪽의 숫자를 입력해야 합니다.)