Skip to content

Commit

Permalink
Fix #3
Browse files Browse the repository at this point in the history
Fix to handle possible ArgumentOutOfRangeException in credential constructor
  • Loading branch information
mvadu committed Mar 25, 2017
1 parent 0e68e7f commit a5af293
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 26 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,6 @@ FakesAssemblies/
# LightSwitch generated files
GeneratedArtifacts/
_Pvt_Extensions/
ModelManifest.xml
ModelManifest.xml
/.vs
/.vs
43 changes: 24 additions & 19 deletions AdysTech.CredentialManager/Credential.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,27 @@ public Credential()
internal Credential(NativeCode.NativeCredential ncred)
{
CredentialBlobSize = ncred.CredentialBlobSize;
CredentialBlob = Marshal.PtrToStringUni (ncred.CredentialBlob,
(int) ncred.CredentialBlobSize / 2);
UserName = Marshal.PtrToStringUni (ncred.UserName);
TargetName = Marshal.PtrToStringUni (ncred.TargetName);
TargetAlias = Marshal.PtrToStringUni (ncred.TargetAlias);
CredentialBlob = Marshal.PtrToStringUni(ncred.CredentialBlob,
(int)ncred.CredentialBlobSize / 2);
UserName = Marshal.PtrToStringUni(ncred.UserName);
TargetName = Marshal.PtrToStringUni(ncred.TargetName);
TargetAlias = Marshal.PtrToStringUni(ncred.TargetAlias);
Type = ncred.Type;
Flags = ncred.Flags;
Persist = (NativeCode.Persistance) ncred.Persist;
LastWritten = DateTime.FromFileTime ((long) ( (ulong) ncred.LastWritten.dwHighDateTime << 32 | (ulong) ncred.LastWritten.dwLowDateTime ));
Persist = (NativeCode.Persistance)ncred.Persist;
try
{
LastWritten = DateTime.FromFileTime((long)((ulong)ncred.LastWritten.dwHighDateTime << 32 | (ulong)ncred.LastWritten.dwLowDateTime));
}
catch (ArgumentOutOfRangeException e)
{ }
}

public Credential(System.Net.NetworkCredential credential)
{
CredentialBlob = credential.Password;
UserName = String.IsNullOrWhiteSpace (credential.Domain) ? credential.UserName : credential.Domain + "\\" + credential.UserName;
CredentialBlobSize = (UInt32) Encoding.Unicode.GetBytes (credential.Password).Length;
UserName = String.IsNullOrWhiteSpace(credential.Domain) ? credential.UserName : credential.Domain + "\\" + credential.UserName;
CredentialBlobSize = (UInt32)Encoding.Unicode.GetBytes(credential.Password).Length;
AttributeCount = 0;
Attributes = IntPtr.Zero;
Comment = null;
Expand All @@ -62,22 +67,22 @@ public Credential(System.Net.NetworkCredential credential)
/// instance.</returns>
internal NativeCode.NativeCredential GetNativeCredential()
{
NativeCode.NativeCredential ncred = new NativeCode.NativeCredential ();
NativeCode.NativeCredential ncred = new NativeCode.NativeCredential();
ncred.AttributeCount = 0;
ncred.Attributes = IntPtr.Zero;
ncred.Comment = IntPtr.Zero;
ncred.TargetAlias = IntPtr.Zero;
ncred.Type = this.Type;
ncred.Persist = (UInt32) this.Persist;
ncred.UserName = Marshal.StringToCoTaskMemUni (this.UserName);
ncred.TargetName = Marshal.StringToCoTaskMemUni (this.TargetName);
ncred.CredentialBlob = Marshal.StringToCoTaskMemUni (this.CredentialBlob);
ncred.CredentialBlobSize = (UInt32) this.CredentialBlobSize;
if ( this.LastWritten != DateTime.MinValue )
ncred.Persist = (UInt32)this.Persist;
ncred.UserName = Marshal.StringToCoTaskMemUni(this.UserName);
ncred.TargetName = Marshal.StringToCoTaskMemUni(this.TargetName);
ncred.CredentialBlob = Marshal.StringToCoTaskMemUni(this.CredentialBlob);
ncred.CredentialBlobSize = (UInt32)this.CredentialBlobSize;
if (this.LastWritten != DateTime.MinValue)
{
var fileTime = this.LastWritten.ToFileTimeUtc ();
ncred.LastWritten.dwLowDateTime = (int) ( fileTime & 0xFFFFFFFFL );
ncred.LastWritten.dwHighDateTime = (int) ( ( fileTime >> 32 ) & 0xFFFFFFFFL );
var fileTime = this.LastWritten.ToFileTimeUtc();
ncred.LastWritten.dwLowDateTime = (int)(fileTime & 0xFFFFFFFFL);
ncred.LastWritten.dwHighDateTime = (int)((fileTime >> 32) & 0xFFFFFFFFL);
}
return ncred;
}
Expand Down
5 changes: 3 additions & 2 deletions CredentialManager.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25420.1
# Visual Studio 15
VisualStudioVersion = 15.0.26228.4
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AdysTech.CredentialManager", "AdysTech.CredentialManager\AdysTech.CredentialManager.csproj", "{ABA77D40-8D43-42EE-BE03-44E4AD93672A}"
EndProject
Expand All @@ -10,6 +10,7 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{EBCE1FF4-81EB-4EC9-A085-2CA0F81100E5}"
ProjectSection(SolutionItems) = preProject
appveyor.yml = appveyor.yml
README.md = README.md
EndProjectSection
EndProject
Global
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ Need: Many web services and REST Urls use basic authentication. .Net does not ha
`ICredential.GetCredential (uri, "Basic")` does not provide a way to get current user security context either as it will expose the current password in plain text. So only way to retrieve Basic auth text is to prompt the user for the credentials and storing it, or assume some stored credentials in Windows store, and retrieving it.

This project provides access to all three
####1. Prompt user for Credentials
#### 1. Prompt user for Credentials
```C#
var cred = CredentialManager.PromptForCredentials ("Some Webservice", ref save, "Please provide credentials", "Credentials for service");
```

####2. Save Credentials
#### 2. Save Credentials
```C#
var cred = new NetworkCredential ("TestUser", "Pwd");
CredentialManager.SaveCredentials ("TestSystem", cred);
```

####3. Retrieve saved Credentials
#### 3. Retrieve saved Credentials
```C#
var cred = CredentialManager.GetCredentials ("TestSystem");
```

###Nuget Package
### Nuget Package
[AdysTech.CredentialManager](https://www.nuget.org/packages/AdysTech.CredentialManager/)

####Latest Download
Expand Down

0 comments on commit a5af293

Please sign in to comment.