Kham – Binary Robot http://binary-robot.com Thu, 22 Aug 2013 21:37:45 +0000 en-US hourly 1 https://wordpress.org/?v=4.6.29 What do margin:5px 0; and margin:5px 0 0; mean? http://binary-robot.com/what-do-margin5px-0-and-margin5px-0-0-mean/ Wed, 27 Mar 2013 21:31:38 +0000 http://binary-robot.com/?p=114 According to Box Model:

  • If there is only one value, it applies to all sides.
  • If there are two values, the top and bottom margins are set to the first value and the right andleft margins are set to the second.
  • If there are three values, the top is set to the first value, the left and right are set to the second, and the bottom is set to the third.
  • If there are four values, they apply to the toprightbottom, and left, respectively.
body { margin: 2em }         /* all margins set to 2em */
body { margin: 1em 2em }     /* top & bottom = 1em, right & left = 2em */
body { margin: 1em 2em 3em } /* top=1em, right=2em, bottom=3em, left=2em */

This is defined by the CSS standard, so it should be consistent across all browsers that implements CSS correctly. For browser compatibilities, check out blooberry’s CSS Support History and quirksmode. According to blooberry, margin was first implemented in IE3, so IE6 should be fine.

]]>
Test your sendmail functionality from within PHP http://binary-robot.com/test-your-sendmail-functionality-from-within-php/ Tue, 26 Mar 2013 01:29:23 +0000 http://binary-robot.com/?p=106 Some people have reported problems with sending mail from PHP.

This appears to be particularly prevalent on Windows servers but also on some Linux and other Unix servers.

The attached scripts are small PHP programs you can install on your web server to test whether it can send email from PHP (and whether you actually receive the mail).

Here’s what to do:

  1. Download testmail.php.
  2. Edit it and enter your own email address where the script tells you.
  3. Upload it to your web server (call it “testmail.php”).
  4. Open the script with your web browser.
  5. If it tells you the mail function failed OR you don’t receive the test message,
  6. repeat the above steps with testmail2.php. If neither work, you can contact your hosting provider and ask them why these simple scripts don’t work.

If testmail.php doesn’t work but testmail2.php does work, you’ll need to set SENDMAIL_F_OPTION to “true” in formmail.php. In the configuration section look for:

PHP Code:
define("SENDMAIL_F_OPTION",false);
and change it to:

PHP Code:
define(“SENDMAIL_F_OPTION”,true);
(You need FormMail version 4.09 or later to use the SENDMAIL_F_OPTION feature.)

If you have version 7.11 or later then set SET_SENDER_FROM_EMAIL to “true” too. Look for:

PHP Code:
define("SET_SENDER_FROM_EMAIL",false);
and change it to:

PHP Code:
define("SET_SENDER_FROM_EMAIL",true);

 

Download –> [download id=”1″]

]]>
Moving your WordPress to a different folder http://binary-robot.com/moving-your-wordpress-to-a-different-folder/ Sun, 10 Mar 2013 19:25:07 +0000 http://binary-robot.com/?p=102

When WordPress is installed, it will only work correctly within the folder that the program was installed in.  In order to move WordPress to a different folder within your account, a few modifications to the coding is required.  For example, you may need to do this if originally you installed WordPress on yourdomain.com, but later decide that you want the WordPress site in yourdomain.com/blog.

How to I move my WordPress site to a different folder?

Moving your WordPress website to a new folder is easy!  There are different methods to complete this task, however this is the way that we recommend:

  1. First, log into your cPanel and access your File Manager.
  2. Next, navigate to the folder that currently has your WordPress files.
  3. Now we will copy all the files. (Not Cut. Until the site is working in the new folder we do not want to remove it from the old folder).  To copy the files, click the Select all button at the top of the page.  Once all the files and folders are selected, click on Copy in the File Manager menu bar.
  4. A new window appears where you will instruct File Manger where to copy your files to.  In the filed provided, type in the new folder that you want your WordPress website moved to and click Copy File(s).
  5. This may take a minute or two, depending on the size of your website, so do not be alarmed if nothing appears to happen for several minutes.
  6. Once it is done, navigate to the folder that you moved your WordPress website to and confirm that the files were copied.
  7. Scroll down to the file called wp-config.php, right click on the file and select Code Edit.

Now that you have opened the wp-config file, you will just need to add two lines of code to let the WordPress software know the new location of the website.  In your wp-config file you will see coding that looks similar to this:

/** MySQL hostname */
define(‘DB_HOST’, ‘localhost’);

/** Database Charset to use in creating database tables. */
define(‘DB_CHARSET’, ‘utf8’);

/** The Database Collate type. Don’t change this if in doubt.
*/ define(‘DB_COLLATE’, ”);

After the coding that looks like the example above, you are going to enter in two lines of code:

define(‘WP_HOME’,’http://example.com/newlocation’);
define(‘WP_SITEURL’,’http://example.com/newlocation’);

Make sure that you keep the formatting exactly the same, and replace example.com/newlocation with the folder that you moved your WordPress website too.  Once you have added the two lines of code, click the Save Changes button at the top of the page.

You can navigate to the new location in your web browser and make sure that it worked.  If it does not appear, then make sure that the added code is correct as the exact syntax and capitalization must be used for it to work properly. Once you have thoroughly tested your site and verified that all files and folders were moved correctly, you can then delete the files and folders in the original folder where WordPress was installed (where you copied from in Step 3 above).

Please note, if you installed WordPress through Fantastico de Luxe, moving your WordPress installation as outlined above will NOT update the Fantastico data file that keeps track of software installations. This will not affect most users, but do keep in mind that if you ever decide to switch to a different software for your site, removal of the WordPress files through Fantastico will not work as expected.

]]>
Macbook Pro LCD Screen : Samsung or LG? http://binary-robot.com/99/ Fri, 08 Mar 2013 05:17:59 +0000 http://binary-robot.com/?p=99 Some people are sticklers for getting the best product/configuration for their money. When it comes to a Macbook Pro; it turns out there is more then one manufacturer that provides LCD screens to apple. The irony is that the best screen happens to be the one that Apple is phasing out due to various legal/competitive reasons.

To find out which screen you have, just do the following.

  1. Open up Terminal, paste this, hit enter:
  2. ioreg -lw0 | grep \”EDID\” | sed “/[^<]*</s///” | xxd -p -r | strings -6
  3. If the panel id starts with LSN, you have a Samsung.
  4. If the panel id starts with LP, you have an LG.

Samsung is preferred as it does not suffer from image retention.

]]>
Enabling Windows Authentication within an IntranetASP.NET Web application http://binary-robot.com/enabling-windows-authentication-within-an-intranetasp-net-web-application/ Fri, 08 Mar 2013 02:41:06 +0000 http://binary-robot.com/?p=73 Problem:

You are building an Intranet web application for your organization, and you want to authenticate the users visiting your site.  Specifically, you want to ensure that they are logged in using a valid Windows account on the network, and you want to be able to retrieve each incoming user’s Windows account name and Windows group membership within your application code on the server.

Discussion:

Authentication is the process of identifying and validating the identity of a client accessing an application.  Put more simply — it is the process of identifying “who” the end-user is when they visit a website.

Authentication is typically used in combination with Authorization — which is the process of figuring out whether the authenticated user has permissions to access a particular page/resource or to perform some action.  For example, when an end-user in a browser tries to access a page, ASP.NET might authenticate the user as “Scott”, and would then run through the configured authorization rules for the requested page to figure out whether “Scott” has permission to access it.

ASP.NET supports multiple ways to authenticate browser users visiting a web application, and implements a flexible set of ways to authorize which permissions they have within the application.

For Internet web applications, the most common authentication scenario to use it called Forms Authentication.  Forms Authentication enables a developer to provide a standard HTML login form within their application, and then validate the username/password an end-user submits against a database or other credential store.  Assuming the username/password combination is correct, the developer can then ask ASP.NET to issue an encrypted HTTP cookie to identify and track the user.

For Intranet web applications, the most common authentication scenario to use is called Windows Authentication.  Windows Authentication avoids the need to create a login form within an application, and does not require end-users to manually enter their username/password credentials to login to the application.  Instead, ASP.NET and IIS can automatically retrieve and validate the Windows username of the end-user visiting the site in a secure way.  The benefit of this approach is that it improves the end-user customer experience since users don’t have to re-type their passwords, and/or maintain separate accounts.  It also allows companies to re-use a common security identity system across their entire corporate networks (Windows clients, servers, file-shares, printers, and web apps).

Solution:

To enable Windows Authentication within an ASP.NET Application, you should make sure that you have “Integrated Windows Authentication” (formerly called NTLM authentication) enabled within IIS for the application you are building.

You should then add a web.config file to the root directory of your ASP.NET application that contains an <authentication> section which sets the mode to “Windows”.

You should also then add an <authorization> section to the same web.config file that denies access to “anonymous” users visiting the site.  This will force ASP.NET to always authenticate the incoming browser user using Windows Authentication – and ensure that from within code on the server you can always access the username and Windows group membership of the incoming user.

The below web.config file demonstrates how to configure both steps described above:

 <configuration>

<system.web>

<authentication mode=”Windows” />

<authorization>
<deny users=”?”/>
</authorization>

</system.web>

</configuration>

Note that the <deny users=”?”/> directive within the <authorization> section above is what tells ASP.NET to deny access to the application to all “anonymous” users to the site (the “?” character means anonymous user).  This forces Windows to authenticate the user, and ensures that the username is always available from code on the server.

Obtaining the Logged-in Username via Code

Once you follow the above configuration steps, you can easily access the logged-in username and role/group mappings for the authenticated user within ASP.NET.  For example, you could use the code-snippet below within an ASP.NET page to easily obtain the username of the visiting user:

Dim username As String
username = User.Identity.Name

The code-snippet above works because there is a “User” property built-in to all ASP.NET pages and user-controls.  If you want to gain access to this user data from within a regular class or business object (which doesn’t have this property provided), you can write code like below to achieve the same result:

Dim User As System.Security.Principal.IPrincipal
User = System.Web.HttpContext.Current.User

Dim username As String
username = User.Identity.Name

The code above obtains the User IPrincipal object for the current request by accessing it via the static HttpContext.Current property that ASP.NET provides (this in turn uses call-context to retrieve it from the active ASP.NET worker thread).  This avoids you having to pass this User object into your business classes directly, and instead allows you to access the User object from anywhere in the application.

Outputting the Logged-in Username on a Page

You can use the username string we retrieved above to programmatically output the username to a page however you want.  For example, you could easily add an <asp:label> control to the page and write code like so to set it:

Label1.Text = “Welcome ” & User.Identity.Name

ASP.NET 2.0 also ships with a built-in <asp:LoginName> control that you can use to declaratively output the user-name to the page:

<asp:LoginName ID=”LoginName1″ FormatString=”Welcome {0}!” runat=”server” />

This control provides an easy way to display the username within the application without having to write any code at all (note the use of the “FormatString” property on the control above – which allows you to easily specify a text mask to use with it).

Looking up Role/Group information for a User

ASP.NET provides a useful “Role Management” capability, which allows developers to map users into logical “Roles” that can then be used to better control end-user capabilities and authorization access.  For example, as a developer I could create a role called “managers” for my web application, and then limit access to portions of the site to only those users within the “managers” role (note: I will be posting additional recipes in the future that discuss how to fully use the Role Management authorization and capabilities features more).

When using Windows Authentication, ASP.NET allows developers to create and populate roles from multiple sources.  For example, a developer could setup the built-in ASP.NET 2.0 SqlRoleProvider to map Windows users to custom application roles that are store within a database.  This approach is very useful for scenarios where there might be application-specific role mappings that don’t make sense to push into a centralized Active Directory tree/store.

ASP.NET also makes it easy to access central Windows and Active Directory group mappings from within an application as well.  For example, if there is a Windows group on the Active Directory network called “DOMAIN\managers”, an ASP.NET application could lookup whether the current Windows authenticated user visiting the ASP.NET site belongs to this group by writing code like this:

If User.IsInRole(“DOMAIN\managers”) Then
Label1.Text = User.Identity.Name & ” is a manager”
Else
Label1.Text = User.Identity.Name & ” is not a manager”
End If

Note that the role/group look-up is done via the “User.IsInRole(rolename)” method that is a peer of the User.Identity.Nameproperty.

Next Steps

Once you understand the basics above, you know how to authenticate and identify Windows users visiting your Intranet application, as well as to lookup what Windows groups and roles they belong to.

In a future Recipe we’ll walkthrough more advanced role-management scenarios, and also discuss ways to authorize and restrict access and capabilities within an ASP.NET application based on the authenticated user’s authorization rights.

]]>
Why use C# for .NET development http://binary-robot.com/why-use-c-for-net-development/ Fri, 08 Mar 2013 02:39:42 +0000 http://binary-robot.com/?p=71 Why we use C# for .NET development

In the company where we work, we have a policy of using Java for all major developments. The core reason behind this being that we have a long term aspiration to being vendor and platform independent.

Regards the .NET framework the policy is to use VB rather than C#. Having spent some time using both langauges I disagree with this decision. I am not one to ignore company policy but I will try to change it where I think it is incorrect.

So, I spent a little time putting a SOAP (summary on a page) together to present to our chief architect.

I expect there are many companies out there with a similar policy, so I thought I would post this so you could share my reasonings and perhaps change your own company policies.

I wouldn’t be appropriate to post the original but here is a public version:

SOAP – .NET Language Preference

Summary

Java is the preferred development platform within our company, but where close coupling with Microsoft technologies is required, development using the Microsoft .NET Framework may be required or preferred.

In an aim to reduce the number of languages supported by our company, a decision has been made to select a preferred language for the .NET platform.

Our company has selected VB .NET as the preferred language for .NET development. The reasoning behind this is that we already have live applications written using VB .NET (although there are also live C# applications within our company). The status of C# is that it may be used where an application is already developed using this language, but not for new projects.

This SOAP proposes switching the status of the VB. NET and C# languages so that C# is the preferred language and existing applications in VB .NET may still be maintained.

Operational Perspective

The reasoning behind this proposal is as follows:

  • VB .NET is significantly different to Java in terms of syntax and structure. C# is based on the Java/C++ syntax. VB .NET developers will have a steeper learning curve when adopting Java than C# developers. For this reason C# lends itself better to being a secondary language in a Java based organisation.
  • The .NET CLR (Common Language Infrastructure) has been submitted to ECMA and accepted. C# has the same status, http://msdn.microsoft.com/netframework/ecma/ . VB .NET has not. C# is better protected than VB .NET to changes in strategic direction.
  • Both VB .NET and C# have been used within our company to develop live applications.
  • Typically developers migrated from VB6 to VB .NET as this reduced the learning curve. However the .NET environment is significantly different from VB6, even though it looks superficially similar. The migration from VB6 to VB .NET can result in poorly designed VB6 style solutions running in the .NET framework.
  • Visual Studio 2005 comes with JLCA (Java Language Conversion Assistant) allowing Java code to be ported to C#, http://msdn.microsoft.com/vstudio/downloads/tools/jlca/ . There is no comparable tool for VB .NET.
  • C# provides the ability to work with C/C++ style pointers. This is rarely required and should be avoided. The exception to this rule is some forms of interoperability with legacy Microsoft code. VB .NET does not have this capability.
  • C# supports Java style code documentation – where definitions of properties and methods are commented in such a way as to make the component interfaces self documenting. VB .NET does not support this.
  • Visual Studio .NET itself was written using C#. Community support for C# is wider than for VB both inside and outside Microsoft, http://blogs.msdn.com/categories , http://www.technorati.com/search/vb c.f.http://www.technorati.com/search/c# . Industry wide there is more demand for C# as shown by the following link: http://www.indeed.com/jobtrends?q=C%23%2C+VB.NET .

Financial Perspective

  • In terms of the cost of multi-skilled developers, the “C# & Java” skillset should be cheaper than “VB & Java” due to the languages similarities. Not only does this relate to less training but also a higher quality of code/output as the developer is more likely to be highly competent in both languages.
  • The lack of case sensitivity in VB means that there can be no common coding standards across Java/VB as there could be across Java/C#. This further increases the cost of cross training and maintaining those skills.
  • The internet is the first stop when a development problem is encountered, and here the significantly larger online C# community reduces time required to resolve problems.
  • The greater industry-wide take up of C# (compared to VB) means that more developers are available to recruit.
  • Most C# developers were previously Java or C/C++ developers, and most VB developers come from a VB5/6 background. As a result the average C# developer will have more background in object oriented design (and is more likely to be to be competent in Java also).
  • I don’t believe that there is much wrong per se with VB, it’s the peripheral aspects such as developer background, community support, and skills availability that make the difference.
  • As a result I suggest that C# developers will, on average, be more productive than their VB counterparts, saving our company money and time.

Conclusion

The differences between VB. NET and C#, as standalone languages, are fairly minimal. C# can do very few things that VB .NET cannot, and VB .NET provides a slightly easier migration path for existing Microsoft VB developers.

However, in the context of Java being the primary language in the organisation, C# is a considerably better fit as a preferred .NET language.

Combined with C#’s status as a ratified standard this provides a strong argument to switch the statuses of VB .NET and C# within the organisation.

….

The argument is under review here still so I would appreciate any constructive commments…

 

source: http://www.geekzilla.co.uk/View026313AC-108F-4EA1-90A2-5E5BE0DEE0F2.htm

]]>
DateTime Patterns http://binary-robot.com/datetime-patterns/ Fri, 08 Mar 2013 02:34:46 +0000 http://binary-robot.com/?p=69 DateTime.ToString() Patterns

All the patterns:

0 MM/dd/yyyy 08/22/2006
1 dddd, dd MMMM yyyy Tuesday, 22 August 2006
2 dddd, dd MMMM yyyy HH:mm Tuesday, 22 August 2006 06:30
3 dddd, dd MMMM yyyy hh:mm tt Tuesday, 22 August 2006 06:30 AM
4 dddd, dd MMMM yyyy H:mm Tuesday, 22 August 2006 6:30
5 dddd, dd MMMM yyyy h:mm tt Tuesday, 22 August 2006 6:30 AM
6 dddd, dd MMMM yyyy HH:mm:ss Tuesday, 22 August 2006 06:30:07
7 MM/dd/yyyy HH:mm 08/22/2006 06:30
8 MM/dd/yyyy hh:mm tt 08/22/2006 06:30 AM
9 MM/dd/yyyy H:mm 08/22/2006 6:30
10 MM/dd/yyyy h:mm tt 08/22/2006 6:30 AM
10 MM/dd/yyyy h:mm tt 08/22/2006 6:30 AM
10 MM/dd/yyyy h:mm tt 08/22/2006 6:30 AM
11 MM/dd/yyyy HH:mm:ss 08/22/2006 06:30:07
12 MMMM dd August 22
13 MMMM dd August 22
14 yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss.fffffffK 2006-08-22T06:30:07.7199222-04:00
15 yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss.fffffffK 2006-08-22T06:30:07.7199222-04:00
16 ddd, dd MMM yyyy HH’:’mm’:’ss ‘GMT’ Tue, 22 Aug 2006 06:30:07 GMT
17 ddd, dd MMM yyyy HH’:’mm’:’ss ‘GMT’ Tue, 22 Aug 2006 06:30:07 GMT
18 yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss 2006-08-22T06:30:07
19 HH:mm 06:30
20 hh:mm tt 06:30 AM
21 H:mm 6:30
22 h:mm tt 6:30 AM
23 HH:mm:ss 06:30:07
24 yyyy’-‘MM’-‘dd HH’:’mm’:’ss’Z’ 2006-08-22 06:30:07Z
25 dddd, dd MMMM yyyy HH:mm:ss Tuesday, 22 August 2006 06:30:07
26 yyyy MMMM 2006 August
27 yyyy MMMM 2006 August

The patterns for DateTime.ToString ( ‘d’ ) :

0 MM/dd/yyyy 08/22/2006

The patterns for DateTime.ToString ( ‘D’ ) :

0 dddd, dd MMMM yyyy Tuesday, 22 August 2006

The patterns for DateTime.ToString ( ‘f’ ) :

0 dddd, dd MMMM yyyy HH:mm Tuesday, 22 August 2006 06:30
1 dddd, dd MMMM yyyy hh:mm tt Tuesday, 22 August 2006 06:30 AM
2 dddd, dd MMMM yyyy H:mm Tuesday, 22 August 2006 6:30
3 dddd, dd MMMM yyyy h:mm tt Tuesday, 22 August 2006 6:30 AM

The patterns for DateTime.ToString ( ‘F’ ) :

0 dddd, dd MMMM yyyy HH:mm:ss Tuesday, 22 August 2006 06:30:07

The patterns for DateTime.ToString ( ‘g’ ) :

0 MM/dd/yyyy HH:mm 08/22/2006 06:30
1 MM/dd/yyyy hh:mm tt 08/22/2006 06:30 AM
2 MM/dd/yyyy H:mm 08/22/2006 6:30
3 MM/dd/yyyy h:mm tt 08/22/2006 6:30 AM

The patterns for DateTime.ToString ( ‘G’ ) :

0 MM/dd/yyyy HH:mm:ss 08/22/2006 06:30:07

The patterns for DateTime.ToString ( ‘m’ ) :

0 MMMM dd August 22

The patterns for DateTime.ToString ( ‘r’ ) :

0 ddd, dd MMM yyyy HH’:’mm’:’ss ‘GMT’ Tue, 22 Aug 2006 06:30:07 GMT

The patterns for DateTime.ToString ( ‘s’ ) :

0 yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss 2006-08-22T06:30:07

The patterns for DateTime.ToString ( ‘u’ ) :

0 yyyy’-‘MM’-‘dd HH’:’mm’:’ss’Z’ 2006-08-22 06:30:07Z

The patterns for DateTime.ToString ( ‘U’ ) :

0 dddd, dd MMMM yyyy HH:mm:ss Tuesday, 22 August 2006 06:30:07

The patterns for DateTime.ToString ( ‘y’ ) :

0 yyyy MMMM 2006 August

Building a custom DateTime.ToString Patterns

The following details the meaning of each pattern character. Note the K and z character.

d Represents the day of the month as a number from 1 through 31. A single-digit day is formatted without a leading zero
dd Represents the day of the month as a number from 01 through 31. A single-digit day is formatted with a leading zero
ddd Represents the abbreviated name of the day of the week (Mon, Tues, Wed etc)
dddd Represents the full name of the day of the week (Monday, Tuesday etc)
h 12-hour clock hour (e.g. 7)
hh 12-hour clock, with a leading 0 (e.g. 07)
H 24-hour clock hour (e.g. 19)
HH 24-hour clock hour, with a leading 0 (e.g. 19)
m Minutes
mm Minutes with a leading zero
M Month number
MM Month number with leading zero
MMM Abbreviated Month Name (e.g. Dec)
MMMM Full month name (e.g. December)
s Seconds
ss Seconds with leading zero
t Abbreviated AM / PM (e.g. A or P)
tt AM / PM (e.g. AM or PM
y Year, no leading zero (e.g. 2001 would be 1)
yy Year, leadin zero (e.g. 2001 would be 01)
yyy Year, (e.g. 2001 would be 2001)
yyyy Year, (e.g. 2001 would be 2001)
K Represents the time zone information of a date and time value (e.g. +05:00)
z With DateTime values, represents the signed offset of the local operating system’s time zone from Coordinated Universal Time (UTC), measured in hours. (e.g. +6)
zz As z but with leadin zero (e.g. +06)
zzz With DateTime values, represents the signed offset of the local operating system’s time zone from UTC, measured in hours and minutes. (e.g. +06:00)
f Represents the most significant digit of the seconds fraction; that is, it represents the tenths of a second in a date and time value.
ff Represents the two most significant digits of the seconds fraction; that is, it represents the hundredths of a second in a date and time value.
fff Represents the three most significant digits of the seconds fraction; that is, it represents the milliseconds in a date and time value.
ffff Represents the four most significant digits of the seconds fraction; that is, it represents the ten thousandths of a second in a date and time value. While it is possible to display the ten thousandths of a second component of a time value, that value may not be meaningful. The precision of date and time values depends on the resolution of the system clock. On Windows NT 3.5 and later, and Windows Vista operating systems, the clock’s resolution is approximately 10-15 milliseconds.
fffff Represents the five most significant digits of the seconds fraction; that is, it represents the hundred thousandths of a second in a date and time value. While it is possible to display the hundred thousandths of a second component of a time value, that value may not be meaningful. The precision of date and time values depends on the resolution of the system clock. On Windows NT 3.5 and later, and Windows Vista operating systems, the clock’s resolution is approximately 10-15 milliseconds.
ffffff Represents the six most significant digits of the seconds fraction; that is, it represents the millionths of a second in a date and time value. While it is possible to display the millionths of a second component of a time value, that value may not be meaningful. The precision of date and time values depends on the resolution of the system clock. On Windows NT 3.5 and later, and Windows Vista operating systems, the clock’s resolution is approximately 10-15 milliseconds.
fffffff Represents the seven most significant digits of the seconds fraction; that is, it represents the ten millionths of a second in a date and time value. While it is possible to display the ten millionths of a second component of a time value, that value may not be meaningful. The precision of date and time values depends on the resolution of the system clock. On Windows NT 3.5 and later, and Windows Vista operating systems, the clock’s resolution is approximately 10-15 milliseconds.
F Represents the most significant digit of the seconds fraction; that is, it represents the tenths of a second in a date and time value. Nothing is displayed if the digit is zero.
: Represents the time separator defined in the current DateTimeFormatInfo..::.TimeSeparator property. This separator is used to differentiate hours, minutes, and seconds.
/ Represents the date separator defined in the current DateTimeFormatInfo..::.DateSeparator property. This separator is used to differentiate years, months, and days.
Represents a quoted string (quotation mark). Displays the literal value of any string between two quotation marks (“). Your application should precede each quotation mark with an escape character (\).
Represents a quoted string (apostrophe). Displays the literal value of any string between two apostrophe (‘) characters.
%c Represents the result associated with a c custom format specifier, when the custom date and time format string consists solely of that custom format specifier. That is, to use the d, f, F, h, m, s, t, y, z, H, or M custom format specifier by itself, the application should specify %d, %f, %F, %h, %m, %s, %t, %y, %z, %H, or %M. For more information about using a single format specifier, see Using Single Custom Format Specifiers.

||\c || Represents the escape character, and displays the character “c” as a literal when that character is preceded by the escape character (\). To insert the backslash character itself in the result string, the application should use two escape characters (“\\”).

Any other character copies any other character to the result string, without affecting formatting. ||

]]>
Globally set the date format in ASP.NET http://binary-robot.com/globally-set-the-date-format-in-asp-net/ Fri, 08 Mar 2013 02:33:18 +0000 http://binary-robot.com/?p=67 change the current thread culture in your Global.asax file, and override the date format for example:

using System.Globalization;
using System.Threading;

//...
protected void Application_BeginRequest(Object sender, EventArgs e)
{    
  CultureInfo newCulture = (CultureInfo) System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
  newCulture.DateTimeFormat.ShortDatePattern = "dd-MMM-yyyy";
  newCulture.DateTimeFormat.DateSeparator = "-";
  Thread.CurrentThread.CurrentCulture = newCulture;
}
]]>
Singleton Class http://binary-robot.com/singleton-class/ Fri, 08 Mar 2013 02:32:08 +0000 http://binary-robot.com/?p=65 The Singleton Design Pattern ensures that only a single instance of a given object can exist.

It does this by making the class constructor private so that it [the singleton itself] has full control over when the class instance is created.  In order to gain access to an instance of a class that implements the Singleton pattern, the developer must call a shared/static method of that Singleton class.

A VB example of a Singleton

Public Class SingletonSample

 

‘shared members

Private Shared _instance As New SingletonSample

 

Public Shared Function Instance() As SingletonSample

Return _instance

End Function

 

‘instance members

Private Sub New()

‘public instantiation disallowed

End Sub

 

‘other instance members

‘…

 

End Class

A C# example of a Singleton

public class SingletonSample

{

//shared members

private static SingletonSample _instance = new SingletonSample();

 

public static SingletonSample Instance()

{

return _instance;

}

 

//instance members

private SingletonSample()

{

//public instantiation disallowed

}

 

//other instance members

//…

}

 

With the Singleton Design Pattern in place, the developer can easily access that single object instance without needing to worry about inadvertently creating multiple instances, and provides a global point of access to it.

VB – Dim mySingleton As SingletonSample = SingletonSample.Instance

C# – SingletonSample mySingleton = SingletonSample.Instance();

]]>
Integrated Active Directory Security in ASP.Net http://binary-robot.com/integrated-active-directory-security-in-asp-net/ Fri, 08 Mar 2013 02:30:45 +0000 http://binary-robot.com/?p=62 This describes how to secure your Asp.Net application if you’re inside an Active Directory environment, and you want to use the integrated sign on, so your users don’t have to remember yet another login/password. How to limit access to your application This explains how to limit access to your application to members of a certain role/group. First you need to change your web.config so that it uses windows authentication, and make it only allow members of a specific Active Directory group. Here is the cut down web.config, showing the changes to the system.web section:

<?xml version="1.0"?> 
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0"> 
  ...etc... 
  <system.web> 
    <authentication mode="Windows" /> 
    <authorization> 
      <allow roles="MYDOMAIN\MyAppUsers" /> 
      <deny users="*" /> 
    </authorization> 
  </system.web> 
</configuration>

Then you want to make a landing page for users who aren’t allowed to log in. This is just a plain HTML page, you can call it ‘noaccess.htm’. Now you want to make it send disallowed people to this ‘noaccess.htm’. Open or create the Global.asax page, and change/add this function:

// This redirects people without access to the noaccess.htm page 
protected void Application_EndRequest(Object sender, EventArgs e) 
{ 
  HttpContext context = HttpContext.Current; 
  if (context.Response.Status.Substring(0, 3).Equals("401")) 
  { 
    context.Response.ClearContent(); 
    context.Response.Write("<scr" + 
      "ipt language=javascript> self.location='noaccess.htm'; </sc" + 
      "ript>"); 
  } 
}

How to figure out who is logged in Quite simple this one – it is stored in this variable, accessible from your aspx.cs code-behind classes (Update – FIXED):

HttpContext.Current.Request.ServerVariables["AUTH_USER"];

Say you’ve got a literal control on your webform, and you want to show the currently logged on user, you could do this:

literalLoggedOnUser.Text = HttpContext.Current.Request.ServerVariables["AUTH_USER"];

How to give different permissions to different users Say you want to give some people more access than others, you’ll want to create another AD group for these users: “MYDOMAIN\MyAppAdmins”. Open up your web.config and add the appSettings section like so:

<?xml version="1.0"?> 
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0"> 
  <appSettings> 
    <add key="AdminGroup" value="MYDOMAIN\MyAppAdmins"/> 
  </appSettings> 
  ...etc... 
</configuration>

The reason for adding the name of the admin group in the web.config is so that if you have different AD groups for the development vs production environments, it’ll be a simple matter of only changing the web.config. The principle adhered to here is that the only difference between development and production should be the web.config file. Then you’ll use this piece of code to check if the current user is in that group:

string group = ConfigurationManager.AppSettings["AdminGroup"]; 
if (User.IsInRole(group)) 
{ 
  // this user is an administrator 
} 
else 
{ 
  // this user is a common pleb 
}

Hint: You’re most likely to place code like that in the Page_Load function for the admin pages. Notes This is for C# ASP.Net 2. Some things may be different with VB.Net or .Net 1.1 Redirect to noaccess.htm:

 

credit: http://www.splinter.com.au/easy-integrated-active-directory-security-in/

]]>