수안이의 컴퓨터 연구실

  • Mainpage
  • About Me
  • Tags
  • Metapage
  • Notice
  • Location
  • Keywords
  • Guestbook
  • Admin
  • Write an Article
  • Total | 1693940
  • Today | 291
  • Yesterday | 588

Web2006/01/24 21:26

닷넷과 리치 클라이언트 이해 2 - 2

김경윤 (redstrato@elasticware.com)

부분 신뢰 코드
여러분이 네트워크에서 배포되는 코드를 작성한다면 항상 코드 액세스 보안을 염두에 두어야 한다. 어떤 영역에서는 실행 권한을 가질 수 있고 다른 영역에서는 그렇지 않을 수도 있기 때문이다. 이럴 때는 미리 코드가 해당 권한을 가지는지 확인하고 거기에 따라 적절한 조치를 해주면 된다. 이는 CodeAccessPermission의 파생 클래스를 이용해 확인할 수 있다. 예를 들어 로컬 머신상의 파일에 대한 접근을 하려 한다면다음과 같이 하면 된다.

try {
FileIOPermission fileIOPerm =
newFileIOPermission(FileIOPermissionAccess.AllAccess, @”c:foo.txt”);
fileIOPerm.Demand();
} catch {
// 필요한 조치를 취한다.
}


앞 코드가 내 컴퓨터 영역에서 실행된다면 아무런 문제가 없을 것이나 ‘인터넷’인 경우에는 SecurityException이 발생해 catch {} 내의 코드를 실행하게 될 것이다.
코드 액세스 보안은 스택에 기초하며 어셈블리 단위로 이뤄진다. 지난 호에서 우리는 윈폼 응용 프로그램에서 웹에 있는 컨트롤을 가져와서 사용한 예제를 보았다. RemoteAssembly Consumer.exe라는 어셈블리가 웹 서버 또는 네트워크에 공유되어 있는 DragAnd DropListBox.dll 어셈블리를 로드해 컨트롤을 사용했다.
만약 DragAndDropListBox.dll의 코드내에 제한된 리소스에 대한 접근을 시도하는 코드가 있었다면 역시 예외가 발생했을 것이다. 런타임의 명령 포인터(instruction pointer)가 가리키는 코드의 권한 검사를 하다가 코드가 인터넷에서 배포된 어셈블리에 위치하는 경우에 필요한 권한이 없으면 예외가 발생할 것이다.
다음과 같이 닷넷 프레임워크에서는 데이터베이스, 네트워크, 파일 I/O, 레지스트리 등의 리소스에 대한 퍼미션을 관리하는 클래스를 제공하고 있다.

System.Security.CodeAccessPermission
System.Data.Common.DBDataPermission
System.Drawing.Printing.PrintingPermission
System.Messaging.MessageQueuePermission
System.Net.DnsPermission
System.Net.SocketPermission
System.Net.WebPermission
System.Security.Permissions.EnvironmentPermission
System.Security.Permissions.FileDialogPermission
System.Security.Permissions.FileIOPermission
System.Security.Permissions.IsolatedStoragePermission
System.Security.Permissions.PublisherIdentityPermission
System.Security.Permissions.ReflectionPermission
System.Security.Permissions.RegistryPermission
System.Security.Permissions.ResourcePermissionBase
System.Security.Permissions.SecurityPermission
System.Security.Permissions.SiteIdentityPermission
System.Security.Permissions.StrongNameIdentityPermission
System.Security.Permissions.UIPermission
System.Security.Permissions.UrlIdentityPermission
System.Security.Permissions.ZoneIdentityPermission


제약 사항과 극복 방법
닷넷에서는 관리되는 코드가 제한된 리소스에 접근하기 위한 훌륭한 방법들을 제공하는데 Common Dialog, IsolatedStorage 등이 그것이다.

파일 I/O
앞서 보았듯이 웹이나 네트워크에서 배포된 코드는 로컬 파일 시스템에 대한 접근이 제한된다. 하지만 방법은 있다. OpenFileDialog, SaveFileDialog를 사용하는 것이다. 언뜻 이해가 가지 않을 수 있겠지만, 다이얼로그를 띄운다는 것은 사용자가 해당 응용 프로그램이 접근할 수 있는 파일을 정한다는 것을 의미한다. 이에 따라 해당 응용 프로그램은 암시적으로 해당 파일에 대한 접근 권한을 가지게 된다. 그러나 여기에도 몇 가지 제약사항은 있다. 해당 파일의 경로를 얻기 위한 코드는 당연히 제한된다.

OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.ShowReadOnly = true
//openFileDialog.RestoreDirectory = true;
// throw SecurityException

if(openFileDialog.ShowDialog() == DialogResult.OK)
{
FileStream fs = (FileStream)openFileDialog.OpenFile();
//FileStream fs = new FileStream(openFileDialog.FileName);
// 예외 발생


앞과 같이 OpenFileDialog의 RestoreDirectory와 FileName 등의 속성이 제한되며 FileName 속성 대신에 OpenFile() 메쏘드를 사용한다.

IsolatedStorage
IsolatedStorage는 응용 프로그램의 상태 정보를 저장하는 데 필요한 저장소를 제공하는 매우 유용한 클래스이다. 닷넷 환경에서는 응용 프로그램 도메인이라는 개념을 도입하여 하나의 프로세스내에서 여러 응용 프로그램을 실행할 수가 있다. IsolatedStorage는 바로 응용 프로그램 도메인 단위로 격리된 저장소를 제공한다. 지난 호에서 소개한 ‘Return of Rich Client : Code Access Security and Distribution Features in .NET Enhance Client-Side Apps’라는 아티클의 예제 중 재미있는 게임이 하나 있는데, 그 예제 코드에서 게임의 상태를 저장할 때 IsolatedStorage를 이용했다. 여기서는 간단한 코드로 설명하겠다. 다음은 응용 프로그램의 상태를 저장하기 위한 파일 스트림을 제공하는 메쏘드다. 이 메쏘드를 통해 얻은 파일 스트림은 기존의 스트림 객체와 동일하게 사용할 수 있다.

privateIsolatedStorageFileStream CreateSettingsStream()
{
IsolatedStorageFile store =
IsolatedStorageFile.GetUserStoreForDomain();
return new IsolatedStorageFileStream(“settings.txt”, FileMode.Create, store);
}


다음의 코드는 앞의 윈폼 응용 프로그램의 일부로서 폼의 상태를 저장하는 메쏘드다.

private void SaveSettings()
{
// Restore so we don‘t store a zero size or location
WindowState = FormWindowState.Normal;

// Save the window location
using( Stream stream = CreateSettingsStream() )
using( StreamWriter writer = newStreamWriter(stream) )
{
WriteSetting(writer, Location);
WriteSetting(writer, ClientSize);
}
}


여기서 using은 지시어가 아닌 문(statement)으로서 괄호안에서 생성된 객체는 using문이 끝날 때 자동으로 객체의 Dispose()를 호출한다. 주로 사용된 후 바로 해제해야 할 리소스에 쓰인다. 객체는 반드시 Idisposable을 구현하는 클래스의 인스턴스여야 한다.

using (expression | type identifier = initializer)
{
...
}


웹 서비스의 이용
부분적으로 신뢰되는 코드는 네트워크 리소스 이용에도 제한을 받는다. 소켓 등을 직접 사용할 수 없고, 웹 서비스 이용에도 제한을 받는다. 하지만 코드 베이스, 즉 다운로드된 URL의 웹 서비스는 이용 가능하다. 간단한 예제를 통해 확인해 보자.
비주얼 스튜디오 닷넷(VS.NET)에서 C# ASP.NET 웹 서비스 프로젝트(CalcService)를 하나 생성해 다음과 같이 웹 메쏘드 Add를 추가한다.

public class Service1 : System.Web.Services.WebService
{
...
[WebMethod]
public int Add(int a, int b)
{



<화면 3> Form1



<화면 4> 추가된 웹 참조


return a+b;
}
}

윈도우 응용 프로그램 프로젝트를 하나 만들어서 <화면 3>과 같이 폼을 작성한다. 그런 다음 웹 서비스를 참조하기 위해 솔루션 탐색기의 ‘참조’를 마우스 오른쪽 버튼으로 클릭하여 ‘웹 참조’를 선택한다. 웹 참조 추가 다이얼로그의 주소 입력 박스에 웹 서비스의 WSDL 문서 URL(http://localhost/CalcService/Service1.asmx? WSDL)를 입력한다. 이 주소는 웹 서비스의 실행 페이지에서 ‘서비스 설명’을 클릭하여 이동한 페이지의 URL이다. 그리고 ‘참조 추가’를 클릭하면 <화면 4>처럼 해당 웹 서비스에 대한 참조를 확인할 수 있다.
일반적으로 웹 서비스를 이용하는 프로그램이라면 다음과 같이 바로 객체(프록시)를 생성해 사용하면 문제가 없겠지만 네트워크에서 배포된 상태라면 분명 예외가 발생할 것이다. 그리고 localhost 대신에 컴퓨터 이름을 사용하는 경우에도 예외가 발생한다.

private void button1_Click(object sender, System.EventArgs e)
{
if ( textBox1.Text == “” || textBox2.Text == “” )
return
localhost.Service1 calcService = new localhost.Service1(); // ①

int a = Convert.ToInt32(textBox1.Text);
int b = Convert.ToInt32(textBox2.Text);
int sum = calcService.Add(a, b);
textBox3.Text = sum.ToString();
}


이 윈폼 프로그램을 자신의 웹 서버에 올려놓고 실행해 보라. 주소를 http://kky/richtest/smartcalculator.exe(여기서 kky는 필자의 컴퓨터 이름)로 주었을 경우에는 <화면 5>와 같은 다이얼로그가 뜰 것이다.


<화면 5> Security Exception 다이얼로그


http://localhost/richtest/smartcalculator.exe로 주었을 경우에는 문제없이 잘 작동한다.
왜냐하면 calcService 객체의 기본 URL이 http://localhost/ CalcService/Service1.asmx이기 때문이다. 그러므로 calcService의 URL 속성 값을 현재 로드된 어셈블리의 코드베이스로 바꿔줘야 한다.

private localhost.Service1 GetCalcService()
{
localhost.Service1 calcService = new localhost.Service1();
try
{
string appBase = AppDomain.CurrentDomain.BaseDirectory;
string site = System.Security.Policy.Site.CreateFromUrl(appBase).Name;
calcService.Url = calcService.Url.Replace(“//localhost/”, “//” + site + “/”);
}
catch
{}
return calcService;
}


그리고 button1_Click()에서 ① 부분의 코드는 다음과 같이 바꿔줘야 한다.

localhost.Service1 calcService = GetCalcService();


그러면 코드는 해당 코드 베이스의 URL로 웹 서비스를 요청하게 되므로 보안 예외를 발생하지 않게 된다. 물론 그 URL에 참조하는 웹 서비스가 있어야 하는 것은 당연하다.
"Web" 카테고리의 다른 글
  • 개발자의 새로운 키, 웹2.0 [1부] (0)2007/04/20
  • 닷넷과 리치 클라이언트 이해 2 - 3 (0)2006/01/24
  • 닷넷과 리치 클라이언트 이해 2 - 2 (0)2006/01/24
  • 닷넷과 리치 클라이언트 이해 2 - 1 (0)2006/01/24
  • 닷넷과 리치 클라이언트 이해 1 - 3 (0)2006/01/21
2006/01/24 21:26 2006/01/24 21:26
Posted by webdizen
No Trackback No Comment

Trackback URL : http://www.webdizen.net/blog/trackback/1668

Leave your greetings.

[로그인][오픈아이디란?]

«Prev  1 ... 1511 1512 1513 1514 1515 1516 1517 1518 1519 ... 3009  Next»

RSS HanRSS
Blog Image
webdizen
이곳은 컴퓨터에 대해 연구하고, 공유하고, 소통하기 위한 연구실입니다. 개인적으로는 OLAP, Data Mining, Semantic Web, Data Modeling에 대해서 연구하고 있습니다.

Categories

전체 (3009)
Webdizen (141)
Life (6)
Diary (16)
Blog (9)
IDEA (2)
Travel (10)
Book (16)
Photo (7)
Movie (8)
Music (14)
Leisure Sports (10)
Funny (6)
Hardware (121)
Software (120)
Windows (5)
Unix & Linux (120)
Installation (5)
Kernel (10)
System (34)
Develop (22)
X-Window (0)
Applicaton (31)
Security (4)
Framework (2)
Hadoop (2)
Programming (804)
Algorithm & Data Structure (1)
Assembly (38)
UNIX/Linux C (95)
C++ (128)
STL (4)
Java (38)
Win32 API (92)
ATL/COM (44)
MFC (151)
.NET (26)
WCF/WPF (4)
C# (28)
Network Programming (17)
Database Programming (12)
OpenGL / DirectX (13)
Multimedia Programming (0)
Game Programming (21)
Parallel Distributed Progra... (0)
Reverse Engineering (0)
Debugging (9)
Python (1)
Ruby (1)
Ruby on Rails (1)
QT (4)
GTK (0)
JSP (0)
PHP (6)
ASP.NET (6)
ASP (2)
Development (28)
Useful Library (2)
Data Modeling (0)
Database (105)
Oracle (4)
MSSQL (41)
MySQL (2)
Data Warehouse (2)
Data Mining (4)
Network (66)
Web (79)
DHTML (4)
XHTML (1)
Javascript (1)
CSS (1)
AJAX (9)
XML (11)
Flex (1)
Silverlight (3)
Security (91)
DoS (1)
Kernel (10)
Scanning (3)
Sniffing (0)
Spoofing (4)
Overflow (28)
Web (11)
Shell (10)
Format String (14)
Window (2)
Embedded (70)
Multimedia (27)
Mobile (14)
Graphic (24)
Management (633)
Knowledge (581)
Hadoop (0)

Notice

  • 메타 블로그 사이트에 등록
  • 새해 맞이 블로그의 변화
  • 블로그 명칭 변경
  • 도메인(www.webdizen.net) 구...
  • TEXTCUBE 1.6.1로 업그레이드...

Tags

  • FK 설정
  • 1970년대
  • 중앙교육연구전산원
  • 데이터
  • XML Web Service
  • 썸씽 스페셜
  • Swing
  • 대한민국
  • JSP
  • iostream
  • 저장 프로시저
  • 게임
  • 아이콘
  • 두뇌생활습관
  • PropertyGrid
  • fprintf
  • Essboard
  • 시바스
  • 화학관
  • 헤네지 XO

Recent Articles

  • 트위터(Twitter)의 시작!.
  • 청년 리더의 조건.
  • 애플의 타블렛 PC - 아이패드....
  • 미래의 인터페이스 - 육감 기....
  • 기초발성법 동영상 강좌.

Recent Comments

  • 학교 과제물중 쓰레드에 대하....
    장진혁 03/17
  • 관리자만 볼 수 있는 댓글입....
    비밀방문자 03/12
  • 상대방의 이야기를 열심히 경....
    DoNuts 03/03
  • Lots of students know techn....
    Bobbi35Shannon 02/25
  • 좋은글 잘 보고 갑니다..
    Und_hacker 01/08

Recent Trackbacks

  • printf,scanf를 이용한 형식....
    yundream의 프로그래밍 이야기 03/10
  • 파일 열기/저장하기 CFileDialog.
    은마군의 나태블록 2009
  • World IT Show 2008.
    상우 :: Oranzie's BLOG 2008
  • cvs서버 설치하기.
    3인3색 2008
  • 속속 공개되는 Google Chart....
    PHP와 Web 2.0 2007

Archive

  • 2010/02 (1)
  • 2010/01 (6)
  • 2009/12 (5)
  • 2009/09 (3)
  • 2009/08 (1)

Calendar

«   2010/03   »
일 월 화 수 목 금 토
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      

Bookmarks

    • Administration
      • IIS.NET
      • NTFAQ
      • OS의 모든 것
      • 리눅스포털
    • Database
      • SQL Server Central
      • SQL Team
    • Development
      • .NET Heaven
      • ASP Alliance
      • ASP.NET 2.0
      • Bullog.net
      • C# Corner
      • C++ (C PlusPlus.com)
      • C++ Reference
      • CodeGuru
      • CodePlex
      • DebugLab
      • Dev Articles
      • Devpia
      • DotNet Junkies
      • DotNet Zone
      • Driver Online
      • GOSU.NET
      • HOONS 닷넷
      • Joinc 팀블로그
      • KOSR
      • MSDN Home Page
      • OSR Online
      • Sky.ph - 개발자 커뮤니...
      • TAEYO.NET
      • The Code Project
      • WindowsClient.net
      • 김상욱의 개발자 Side
      • 조인시 위키
    • Human Networks
      • belief21c's e-space
      • I think I can
      • Invisible Rover's Blog :D
      • Rodman®
      • ■ Feel So Good~! ■
      • 까만 나비
      • 나를 가꾸는 시간.
      • 나만의 즐거움~~!
      • 단녕
      • 상우 :: Oranzie's BLOG
    • Information Technology
      • Microsoft TechNet
      • 지디넷코리아 - 글로벌...
    • Security
      • FoundStone
      • milw0rm
      • NewOrder
      • OpenRCE
      • Phrack.org
      • Reverse Engineering b1...
      • Reverse Engineering Team
      • RootKit
      • SecurityFocus
      • SecurityXploded by Nag...
      • Wow Hacker
      • Zone-H
Textcube
Louice Studio Inc.
Powered by Textcube. Original designed by Tistory.