Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Security > Connecting to a UNC share from a web application

Reply
Thread Tools

Connecting to a UNC share from a web application

 
 
Rasmus
Guest
Posts: n/a
 
      01-06-2005
Basically I want to do the following:

DirectoryInfo baseDirInfo = new DirectoryInfo(@"\\server2\files");
FileInfo[] aFileInfos = baseDirInfo.GetFiles();
string ss = "";
foreach (FileInfo fileInfo in aFileInfos){
ss += fileInfo.Name;
}


But I need a way to supply the username and password – otherwise I’m not
allowed to access the share.

Info:
The web application has anonymous access
No domain
A local user is created on server2
This user has access to share d:\files on server2

 
Reply With Quote
 
 
 
 
Paul Clement
Guest
Posts: n/a
 
      01-06-2005
On Thu, 6 Jan 2005 07:59:06 -0800, Rasmus <> wrote:

¤ Basically I want to do the following:
¤
¤ DirectoryInfo baseDirInfo = new DirectoryInfo(@"\\server2\files");
¤ FileInfo[] aFileInfos = baseDirInfo.GetFiles();
¤ string ss = "";
¤ foreach (FileInfo fileInfo in aFileInfos){
¤ ss += fileInfo.Name;
¤ }
¤
¤
¤ But I need a way to supply the username and password – otherwise I’m not
¤ allowed to access the share.
¤
¤ Info:
¤ The web application has anonymous access
¤ No domain
¤ A local user is created on server2
¤ This user has access to share d:\files on server2

Take a look at the below link concerning delegation scenarios:

http://msdn.microsoft.com/library/de...delegation.asp


Paul ~~~
Microsoft MVP (Visual Basic)
 
Reply With Quote
 
 
 
 
Rasmus
Guest
Posts: n/a
 
      01-06-2005
Thank you for the reply.

I've tried the approach from
http://msdn.microsoft.com/library/de...onateTopic.asp

But keep getting "Error: [1326] Logon failure: unknown user name or bad
password."

I'm trying to connect to a XP with a shared folder

 
Reply With Quote
 
Rasmus
Guest
Posts: n/a
 
      01-07-2005
Server name: server2
unc: \\server2\junk
username: test
password: pass

More info on what i can do and not:

In cmd window:
net use x: \\server2\junk /user:server2\test pass

this will map the unc to drive x - and everything works fine.

But using the same username password in the code snippet from
http://msdn.microsoft.com/library/de...onateTopic.asp

I get:
Enter the name of the domain on which to log on: \\server2\junk
Enter the login of a user on \\apollo6\ros that you wish to impersonate:
server2\test
Enter the password for server2\test: pass
LogonUser called.
LogonUser failed with error code : 1326

Error: [1326] Logon failure: unknown user name or bad password.


Exception occurred. Access is denied




please help
 
Reply With Quote
 
[MSFT]
Guest
Posts: n/a
 
      01-07-2005
Hello,

I think you may try impersonate in ASP.NET. This can make your ASP.NET app
act as a designed user and use this user's permission to access network
share. This can be implement with either asp.net config file or code. For
details, you may refer to:

How to implement impersonation in an ASP.NET application
http://support.microsoft.com/default...;EN-US;Q306158

Luke

 
Reply With Quote
 
Rasmus
Guest
Posts: n/a
 
      01-07-2005
Hello,

Thanks for the reply. But i still cant get it to work. with the following
code:



using System;
using System.Runtime.InteropServices;
using System.Security.Principal;

namespace nws
{
/// <summary>
/// Summary description for WebForm2.
/// </summary>
public class WebForm2 : System.Web.UI.Page
{
public const int LOGON32_LOGON_INTERACTIVE = 2;
public const int LOGON32_PROVIDER_DEFAULT = 0;

WindowsImpersonationContext impersonationContext;

[DllImport("advapi32.dll")]
public static extern int LogonUserA(String lpszUserName,
String lpszDomain,
String lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken);
[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public static extern int DuplicateToken(IntPtr hToken,
int impersonationLevel,
ref IntPtr hNewToken);

[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public static extern bool RevertToSelf();

[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
public static extern bool CloseHandle(IntPtr handle);

public void Page_Load(Object s, EventArgs e) {
if(impersonateValidUser("test", "server2", "pass")) {
Response.Write("HURRA");
//Insert your code that runs under the security context of a specific
user here.
undoImpersonation();
}
else {
Response.Write("BUMMER");
//Your impersonation failed. Therefore, include a fail-safe mechanism
here.
}
}

private bool impersonateValidUser(String userName, String domain, String
password) {
WindowsIdentity tempWindowsIdentity;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;

if(RevertToSelf()) {
if(LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT, ref token) != 0) {
if(DuplicateToken(token, 2, ref tokenDuplicate) != 0) {
tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
impersonationContext = tempWindowsIdentity.Impersonate();
if (impersonationContext != null) {
CloseHandle(token);
CloseHandle(tokenDuplicate);
return true;
}
}
}
}
if(token!= IntPtr.Zero)
CloseHandle(token);
if(tokenDuplicate!=IntPtr.Zero)
CloseHandle(tokenDuplicate);
return false;
}

private void undoImpersonation() {
impersonationContext.Undo();
}


#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}

 
Reply With Quote
 
Paul Clement
Guest
Posts: n/a
 
      01-07-2005
On Thu, 6 Jan 2005 12:53:01 -0800, Rasmus <> wrote:

¤ Thank you for the reply.
¤
¤ I've tried the approach from
¤ http://msdn.microsoft.com/library/de...onateTopic.asp
¤
¤ But keep getting "Error: [1326] Logon failure: unknown user name or bad
¤ password."
¤
¤ I'm trying to connect to a XP with a shared folder

Assuming here that you're using a local account and that it exists on both machines and has the same
password?


Paul ~~~
Microsoft MVP (Visual Basic)
 
Reply With Quote
 
Rasmus
Guest
Posts: n/a
 
      01-07-2005
Hi Paul,

No i did not have the test user on both machines. I tried it and it still
did not work.
I noticed this in the code comment:
"This sample can be run only on Windows XP. "

The client in my case is a windows server 2003 - could that be the cause?
 
Reply With Quote
 
Paul Clement
Guest
Posts: n/a
 
      01-07-2005
On Fri, 7 Jan 2005 08:07:05 -0800, Rasmus <> wrote:

¤ Hi Paul,
¤
¤ No i did not have the test user on both machines. I tried it and it still
¤ did not work.
¤ I noticed this in the code comment:
¤ "This sample can be run only on Windows XP. "
¤
¤ The client in my case is a windows server 2003 - could that be the cause?

Have you tried changing the Anonymous account under which the web application is running (it's
probably IUSR_machinename, which can't be delegated) and then just using the standard impersonation
(web.config setting for your ASP.NET app)? If this works then it would eliminate the need for the
impersonation code you've written.

In any event, for delegation to function properly, the local account must exist on both machines and
have the same password.

I'm not sure what impact Windows Server 2003 would have in this configuration. It's a more secure OS
that was designed to operate in a domain based environment.


Paul ~~~
Microsoft MVP (Visual Basic)
 
Reply With Quote
 
[MSFT]
Guest
Posts: n/a
 
      01-10-2005
It seems there is no problem with the code. As Paul suggested, have you
checked the anonymous access in the IIS security setting? Also, how did you
set the authentication in web.config?

Luke

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Share-Point-2010 ,Share-Point -2010 Training , Share-point-2010Hyderabad , Share-point-2010 Institute Saraswati lakki ASP .Net 0 01-06-2012 06:39 AM
Running an asp.net application on a remote share (UNC) - parser er =?Utf-8?B?cGpfc2VydmFkbWlu?= ASP .Net 2 07-01-2005 05:21 PM
Asp.Net Web Service accessing file on UNC share BrianS ASP .Net Security 2 02-11-2005 03:27 PM
Connect to a netword share via UNC name Gaetano D'Aquila ASP .Net 0 01-19-2004 04:47 PM
Trouble on creating a new web project - UNC share does not exist or you do not have access Simon Chung-Jen Chuang ASP .Net 1 08-23-2003 11:01 AM



Advertisments
 



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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57