Archive for October, 2009

Visual Studio 2008 keyboard shortcuts

Sometime back Bryan posted a blog in which he detailed his favorite shortcuts. While browsing through the net I found this good poster which details all the shortcuts.

Keyboard Shortcuts in Visual Studio (C#)

Hope this helps.

Happy Programming!!!

Cheers,

Raja

Advertisements

NetworkCredential in SMTP mail (Forms Authentication)

In the project I am currently working on we have to send email confirmation to the registered user giving them their User ID and Password (yup…..you saw that right password….not secured but that is the requirement :-(). we created a no-reply account and to our amazement all emails sent inside our domain were reaching fine but external domains were not. My hardware tech lead (Mihai) helped me out in checking exchange server(since I didnt have access to it). We saw that no mails to external domain was even queued in exchange. Then Mihai came up to me and asked me to check the credentials which was used for sending email and that was the missing piece :-). Prior to this application all our apps were windows authenticated so there was no need to specify the Network Credentials but in this app we have to use forms authentication since it is an internet site not an intranet site. So added the following line and it worked like charm 🙂


System.Net.NetworkCredential objAuthInfo = new NetworkCredential("<username>", "<password>");

objSMTP.Credentials = objAuthInfo;

Hope this helps.

Happy Programming!!!

Cheers,

Raja

ASP.Net Hybrid Authentication

We are working on a large .Net website that will have users both on and off of our domain.

  • We want to use the ASP.Net Membership framework
  • We want Windows authentication for people on our network
  • All users will be required to have an account (If a windows user on our domain does not have and account (A generic log on ‘kiosk01’) they should receive the Forms logon page
  • We do not want people with Windows accounts to be able to log on outside of our domain. (An employee here should not be able to log on as an employee from home)

This was actually very easy to setup.

First we setup the website. It should have two folders for authentication.

image

The WebLogin folder should take the default security setting from the websites web.config

image

As you can see we are using Forms authentication for the whole site and setting the logon URL to the WinLogin folder.

We create an HTML file (401-2.htm) containing a redirect to the WebLogin page. This will handle the 401-2 access denied error thrown when a user who can’t authenticate because the are off the domain or don’t have a ASP.Net account.

image

(You may want to do your redirect with aspx and code behind instead of html, if you don’t want to hard code the redirect address)

The ‘Weblogin/Default.aspx’ page should be a aspx page with a standard or customized Asp.Net Login control.

In IIS the site level authentication should be set like this…

image

 

Now for the WinLogin folder only, we set the Authentication like this…

image

Right click WinLogin and edit its properties alone

Now while still on WinLogin properties set a custom error HTTP Error 401;2 pointing to the 401-2.htm file created earlier.

image

 

Now all that is left is to write the code for the users who where able to Authenticate to the WinLogin page.

image

Add what ever custom logic you want for your windows authentications.

As you can see we don’t need to do anything with the password. If this code is executing then the user has Authenticated and we are calling the FormsAuthentication.RedirectFromLoginPage method with just the username and the createPersistantCookie boolean variable alone. The call to the RedirectFromLoginPage method  is also actually logging the user in. This confused me at first.

If the asp.net user account is not found (as in our generic log on ‘domain\kiosk01’) the user will be directed to the forms logon.

Now you can create asp.net membership accounts for the domain users assigning them roles etc. Just make sure the user name includes the domain and userid, ‘mydomain\j.user.01’ You can create a randomly generated password for the membership account preventing the domain user from logging in without the Windows authentication. (You would also have to have logic to prevent these users from resetting there password.)

And that’s it. The best of both worlds without making things to complicated.

-ctrlShiftBryan

Impressed with Jquery UI image manipulation

We are using JQuery UI for our current project. I am particularly impressed with their image manipulation using CSS. I am talking about the Framework Icons which is one image ( 5  Kb) but it can be manipulated in such a way that the coordinates are changed to just bring a particular image portion up front. They are using a pretty effective technique using the background-position. I have heard of this technique before but was really excited to see that in action. Let me give you an example:  If you want to use the delete image (trash can) then you can use the following class:

“ui-icon ui-icon-trash” (CSS : .ui-icon-trash { background-position: -176px -96px; }).

so just mouseover the framework icon in the link above, get the class name and use it. The required image would appear. Pretty sweet.

Hope this helps.

Happy Programming!!!

Cheers,

Raja

Scripting Role and User Permissions

I recently had to copy a large number of databases to a development environment. There was several hundred gigabytes of files, so I went with stop sql services, copy, restart services and attach new databases route.

Before copying the files, I migrated the users. After attaching the database files to the new instance, I ran the orphaned user scripts. Everything seemed to have swimmingly.

A couple of hours later, a developer called me up and said that they were running into permission errors executing stored procedures. There were a lot of scripts out there for user permissions, but none of them seemed to fit my specific needs, especially since this was SQL 2000. So, on that note, here is my addition to that myriad of scripts for SQL Server permissions.

/* SQL 2005/2008 */
DECLARE
 @Login varchar(50),
 @Role varchar(50);

SET @Login = NULL;
SET @Role = NULL;

SELECT
 perm.state_desc
 + SPACE(1)
 + perm.permission_name
 + ' on ['
 + s.name
 + '].['
 + o.name
 + '] to ['
 + prin.name
 + '];'
 + CHAR(10)
 + 'GO'
 + CHAR(10)
 COLLATE LATIN1_General_CI_AS
FROM
 sys.database_permissions perm
INNER JOIN
 sys.objects o ON
 perm.major_id = o.OBJECT_ID
INNER JOIN
 sys.schemas s ON
 o.SCHEMA_ID = s.SCHEMA_ID
INNER JOIN sys.database_principals prin ON
 perm.grantee_principal_id = prin.principal_id
WHERE
 ((prin.type = 'U' AND prin.name = @Login) OR @Login IS NULL) AND
 ((prin.type = 'R' AND prin.name = @Role) OR @Role IS NULL);

/* SQL 2000 */
DECLARE
 @Login varchar(50),
 @Role varchar(50),
 @ObjectName varchar(50);

SET @Login = NULL;
SET @Role = NULL;
SET @ObjectName = '[admin_getStoreOrderTimes]';

SELECT
 CASE ProtectType
 WHEN 204 THEN 'GRANT_W_GRANT'
 WHEN 205 THEN 'GRANT'
 WHEN 206 THEN 'DENY' END
 + SPACE(1)
 + CASE Action
 WHEN 26 THEN 'REFERENCES'
 WHEN 178 THEN 'CREATE FUNCTION'
 WHEN 193 THEN 'SELECT'
 WHEN 195 THEN 'INSERT'
 WHEN 196 THEN 'DELETE'
 WHEN 197 THEN 'UPDATE'
 WHEN 198 THEN 'CREATE TABLE'
 WHEN 203 THEN 'CREATE DATABASE'
 WHEN 207 THEN 'CREATE VIEW'
 WHEN 222 THEN 'CREATE PROCEDURE'
 WHEN 224 THEN 'EXECUTE'
 WHEN 228 THEN 'BACKUP DATABASE'
 WHEN 233 THEN 'CREATE DEFAULT'
 WHEN 235 THEN 'BACKUP LOG'
 WHEN 236 THEN 'CREATE RULE' END
 + SPACE(1)
 + 'ON'
 + SPACE(1)
 + '['
 + USER_NAME(o.uid)
 + '].['
 + OBJECT_NAME(o.id)
 + ']'
 + SPACE(1)
 + 'TO'
 + SPACE(1)
 + '['
 + u.name
 + '];'
 + CHAR(10)
 + 'GO'
 + CHAR(10)
FROM
 sysusers u
INNER JOIN
 sysprotects p ON
 u.uid = p.uid
INNER JOIN
 sysobjects o ON
 p.id = o.id
WHERE
 ((IsLogin = 1 AND u.name = @Login) OR @Login IS NULL) AND
 (((u.IsSQLRole = 1 OR u.IsAppRole = 1) AND u.name = @Role) OR @Role IS NULL) AND
 (OBJECT_NAME(o.id) = @ObjectName OR @ObjectName IS NULL) AND
 u.name NOT IN ('public','db_owner','db_accessadmin','db_securityadmin','db_ddladmin',
 'db_backupoperator','db_datareader','db_datawriter','db_denydatareader',
 'db_denydatawriter','guest');

~Ron

Script Column Default Values

Another database developer had copied several database from our model office back to our development environment. When we started to wire up out applications I noticed that there were no indexes, defaults, keys, or constraints of any kind. Just heaps.

Adding these constraints is fairly straigtforward, and there are many tools out there to do that. But I could not find a tool that would script just the default constraints without also dropping and/or creating the table. It took a couple of days to get the databases moved over in the first place, but there were over a thousand default constraints, which could also take several days if done manually.

I was sure that there was a way to script this, but after over an hour of searching I decided that I burned enough time and that I would write one myself. I don’t know how often anyone would come across this issue, but here it is anyways.

SELECT
    'ALTER TABLE dbo.' + OBJECT_NAME(s.id) + ' ADD CONSTRAINT DF_' + OBJECT_NAME(s.id) + '_' + sc.name + ' DEFAULT ' + c.text + ' FOR ' + sc.name
FROM
    dbo.sysconstraints s
INNER JOIN syscolumns sc
	ON s.id = sc.id AND
	   s.colid = sc.colid
INNER JOIN dbo.syscomments c
    ON s.constid = c.id
WHERE
    s.status & 5 = 5