Tips and Tricks in a world of Mix

Archive for the ‘Asp.net’ Category

Encrypt–Decrypt connectionString

public static void Encrypt(){

           Configuration objConfiguration = null;

           if(HttpContext.Current!=null)        //if we are in web application of some sort

               objConfiguration = WebConfigurationManager.OpenWebConfiguration (“~”);

           else       ///if not web

                objConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

           ConfigurationSection objSection = “connectionStrings”;

           //if encrypted already don’t do it again

           if(objSection!=null && !objSection.SectionInformation.IsProtected){

                           objSection.SectionInformation.ProtectionSection(string.Empty);

                           objSection.SectionInformation.ForseSave = true;

                          objConfiguration.Save(ConfigurationSaveMode.Full);

           }     

}

 

public static void Encrypt(){

                     Configuration objConfiguration = null;

                       if(HttpContext.Current!=null) //if we are in web application of some sort

                                   objConfiguration = WebConfigurationManager.OpenWebConfiguration (“~”);

                       else ///if not web

                                    objConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

                      ConfigurationSection objSection = “connectionStrings”;

                           //if encrypted then decrypt

                            if(objSection!=null && !objSection.SectionInformation.IsProtected){

         objSection.SectionInformation.UnprotectSection();

  //objConfiguration.Save();  – you can save the change but it will reset all the global data of your application in iis (relevant to comment it for web applications,can stay on in others)

}

}

Advertisements

Setting constant parameter in WebAPI

So the mission was to set static member for connectionString , so that we’ll get the string from web.config just once. On the way we did Encrypt and Decrypt of the connectionString.

So the trouble was that we couldn’t be sure that the static field is staying or going on the request- response model of WebApi.

The easy solution was to reuse the existing code to init HttpContext.Current.Application[“connectionString”] value if it’s null at the global.asax at Application_Start function.

When initialized in that function at the host/server side of the distributed system , the HttpContext.Current.Application[“connectionString”] is initialized for the first time and until the IISReset.

If you want something constant for one request the way to go is to put it in Init function at gobal.asax 

The trouble was that the Application variable has been resetting itself each request.

The solution was that because of decryption that has been saving the web.config it has been resetting the site values held by the IIS, so actually it has been recycling the site data resetting the Application variables values also.

At the end the encryption has been executed on publish to the server, the decrypt has been decrypting the secured section but hasn’t been saving it back , so the web.config actually haven’t changed throughout running host , so the Application data kept intact.

WTF happened to my WCF ?–Learn to handle the unknown – debug wcf

As a note, you need to learn how to use the WCF logging utilities:

Logging info. – you have a recommended settings just to copy into web.config-

it’s opens in Microsoft service trace viewer that I’ve written about before.

Config Editor (makes it a snap to setup).

Trace viewer. Totally awesome. Allows multiple services (client and server) to trace and can join them and help you analyze all the details. Lets you get to the root of issues really fast. (Cause when there’s a server WCF error, the client is unlikely to get useful data out.)

 

source – http://stackoverflow.com/questions/204032/complex-data-types-in-wcf

There is a duplicate ‘system.web.extensions/scripting/scriptResourceHandler’ section defined

Details:

Filename: \\?\c:\dev\PreCompAdmin\web.config

Line number: 6

Error: There is a duplicate ‘system.web.extensions/scripting/scriptResourceHandler’ section defined

 

—————————
OK  
—————————

http://forums.asp.net/t/1571308.aspx/1

This is a known issue with ASP.NET 4.0 / vs 2010

See http://www.asp.net/learn/whitepapers/aspnet4/breaking-changes#0.1__Toc256770149

"The workaround for the second scenario is to delete or comment out all the system.web.extensions configuration section definitions and configuration section group definitions from the application-level

Web.config
file. These definitions are usually at the top of the application-level
Web.config
file and can be identified by the configSections element and its children."

Managing External Dlls–Make Your Project Portable and Independent

   I think that any programmer have tackled with this problem of missing dlls , or suddenly the dlls are upgraded in the server and you are left with your application to pick up the pieces.

  SO , how can we handle the dlls so that our application won’t be effected by almost anything?

  First of all , it is important to understand that the solution presented here does not cancel the need to install the Framework on the destination machine, meaning that the correct framework must be there always in order for your application to work properly.

  There are few solutions for managing your dlls :

1)Reference to GAC as usual

2)Add a folder to specific project with your needed dlls

3)Add a solution folder to your solution and locate your needed dlls there

4)Locate your dlls on some Shared Folder in your organization

5)Build a new Project and insert the dlls there

 

 

In each and every solutions there are Pros and Cons as always :

method pros cons
1)Reference to GAC as usual You don’t need to worry about anything, it’s all in the system. If you are using standard Microsoft dlls , even if there will be an upgrade of the dlls version , Microsoft always tries to make it backward compatible – it won’t touch you. If you have some specific dlls , such as Oracle.DataAccess.dll etc. then it can happen that the dll that you have on your terminal , that you develop with is a different version – in this case when you publish your application on the server your application won’t work properly. You’ll get mistakes of bad assembly reference or one of it dependencies.
2)Add a folder to specific project It’s easy, and whenever you deploy the project you are carrying your dlls with you. If you have a few projects in your solution you can’t use it properly between the projects. You need to keep the location of the folder specific.You could reference the dlls for your current project from a different project folder but when you’ll take the current project as a reference to some third component and then one of the dlls change – you’ll need to recompile all components.
3)Add a solution folder to your solution and locate your needed dlls there It’s a logical unit just holding the dlls – you are adding a logic meaning composing the dlls by logical themes. When you work with TFS or VSS or any source safe you need to have a physical object of a folder. Another thing – if you have a lot of dlls and you need to drag and drop a whole folder of them into solution folder – you CAN’T, so you’ll have to create every folder by hand inside that solution folder and then import dlls files , folder by folder.
4)Locate your dlls on some Shared Folder in your organization That’s one of the “good” solutions if you work in organization with LAN , and ability to control permission over domains . You just throw the dlls to some folder and share it with every one you like. If there are multiple users on the same dll , the system creates copies and you won’t get stacked. The down side is handling the source safe again . It creates decentralization of your application. Another thing , in terms of maintenance  you can always forget the specific location of the shared folder or not get enough permissions after system team will perform some update on the server.
5)Build a new Project and insert the dlls there SO, the great solution yet is to add a new Class Library project , drag and drop the folders you need to take with you and recompile.
Any component in your solution will reference the  autonomic defined component . You can manage it under your source safe and you can enter it in your deployment package – leaving the hierarchy as predefined at the begging. If the dlls change – all you’ll need to do is to throw the External Dlls project to the server.
When you create the project and drag and drop the folders you’ll get “Project file must include the .NET Framework assembly ‘WindowsBase, PresentationCore, PresentationFramework’ in the reference list.”  error . It means that your dlls are defined as a Content . You need to go through the folders , choose the dlls(can choose list of them) and in properties change the Build Action to Resource. That’s it.

 

So , in this review I’ve shown the best way to make your project portable and independent.

Now you can enjoy the deployment. Cheers!

Comparing ListView with GridView,DataList and Repeater

 

 

The ListView control is a new data presentation control that was added in ASP.Net 3.5.You may wonder why its added to the framework , and what it provide .

From what i have seen, ListView control was added to provide The following functionalities :

  1. A very flexible and customizable layout.
  2. A built in data paging support with the new DataPager control.
  3. Support data grouping (repeating items) in a flexible way.
  4. Built in support for deleting,inserting,paging,sorting,and updating the data.

Now , to compare the ListView control with the dataList,GridView and repeater control , lets look at the table below :

Supported Funcationalities

image

* Supported: means that it’s provided out of the box without any custom code or hacks.

* Not Supported: means that it’s not provided out of the box by the control but it could be possible to implement it using custom code \ hacks.

The GridView : it supports paging but it doesn’t provide a flexible layout , since its mainly used to display the data in a table based layout.And If we looked at data inserting , the Gridview doesn’t have a built in support for inserting data( since it doesn’t call the insert method of it underlying data source when you click on a button with a CommadName set to "Insert" ).

The DataList : it support data grouping ( through its RepeatColumns property) , but it doesn’t have a built in support for paging,inserting ,deleting , updating the data. and if you looked at its laout , you will find that by default  the datalist renders as html table and you will have to set its flowLayout to "Flow" to stop that behaviour.

The Repeater control : you will find that it provides a flexible layout but it doesn’t support data grouping ,inserting,deleting , updating  and paging through the data .

Summary :

The ListView control was added to provide a rich data control that can support all the required functionalities at the same time , so now you can easily display a fully customizable layout that supports Grouping,paging , inserting , deleting , updating and sorting the data.


* Supported: means that it’s provided out of the box without any custom code or hacks.

* Not Supported: means that it’s not provided out of the box by the control but it could be possible to implement it using custom code \ hacks.

The GridView : it supports paging but it doesn’t provide a flexible layout , since its mainly used to display the data in a table based layout.And If we looked at data inserting , the Gridview doesn’t have a built in support for inserting data( since it doesn’t call the insert method of it underlying data source when you click on a button with a CommadName set to "Insert" ).

The DataList : it support data grouping ( through its RepeatColumns property) , but it doesn’t have a built in support for paging,inserting ,deleting , updating the data. and if you looked at its laout , you will find that by default  the datalist renders as html table and you will have to set its flowLayout to "Flow" to stop that behaviour.

The Repeater control : you will find that it provides a flexible layout but it doesn’t support data grouping ,inserting,deleting , updating  and paging through the data .

Summary :

The ListView control was added to provide a rich data control that can support all the required functionalities at the same time , so now you can easily display a fully customizable layout that supports Grouping,paging , inserting , deleting , updating and sorting the data.

Article Source –http://weblogs.asp.net/anasghanem/archive/2008/09/06/comparing-listview-with-gridview-datalist-and-repeater.aspx

HttpContext.Current.User.Identity.Name is Empty

OS: windows 7 x64 bit

IIS: 7.5

run –> inetmgr –> click your website –> enter in features view Authentication –>

 

image

 

Enable Windows Authentication

Disable Anonymous Authentication

image

Pay attention that you did this definitions on your website specificly!!

Another solution is to add to web.config in the system.web section:

<authentication mode="Windows" />

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

 

Now you”l get your Identity back!

 

(if you don’t have the right Authentication options then :

Turn Windows features on or off

image

image

 

image

Turn Windows features on or off  –> IIS –> WWWS –> Security –> Basic, Digest and Windows.

 

At the end restart your machine for the changes to take effect.

Now do the beginning of the post. )

Tag Cloud

%d bloggers like this: