如何把ASP.NET網頁部署至AZURE

Azure簡介

微軟的雲端作業系統。登入https://manage.windowsazure.com/或進到Azure官網點右上角的Portal。

目前Azure支援的有Web Sites, Cloud Services, SQL Databases, Storage。

Web Sites : 靜態網頁。只態使用IIS。

Cloud Services : 動態內容。即Web Application, Web Service等。

SQL Database : 資料庫。

Storage : 類似Google Big Table,好像還可以儲存檔案。

ASP.NET web application

會用到SQL Database和Cloud Service。

SQL DATABASES

首先建立一個Database,Collation目前使用SQL_Latin1_General_CP1_CI_AS。透過SQL的[N””]可儲存中文,例如INSERT INTO table (column) VALUES (N’中文內容’)。其他參數跟付費內容有關。

建立後系統會產生一個domain,例如我們在東亞的abc1234def.database.windows.net,這個可以用ssms登入管理。頁面上有一個[Show connection strings]可以顯示連結字串,預設限定tcp,不能使用namepipe,而且有firewall,要在firewall加入允許規則才可以連線。防火牆設定在SQL Databases->Servers Tab->選一個instance->Configure

SQL schema請使用Sql Migration Wizard從Local Database上傳至Azure Sql Database

CLOUD SERVICES

先建立一個instance,quick create就可以,有兩種模式,production和staging,測試中就選用staging。接下來要先把Web Application打包,deploy後才能再作後續設定。

VS2010

下載並安裝Azure SDK。在原本的Solution底下新增一個Azure Project,不要選擇任何web role->完成。[以下稱Azure Project]

產生空白專案後,在Solution Explorer->Roles上按右鍵,選Web Role in solution,把你想deploy到Azure的web application project加進去[以下稱WebApp]。在WebApp加入Microsoft.WindowsAzure.ServiceRuntime這個參考。

接下來要調整web.config。可以透過在Azure Project按右鍵->屬性設定,也可以在cscfg和csdef這兩個xml手動設定。Csdef會定義cdcfg裡有甚麼設定值(session),實際運行用的參數值會在cscfg。大概只會用到connectinstring。Connectionstring請參考在SQL DATABASE一節的取得方法。

修改Source Code

Azure不支援ConfigurationManager物件,請把所有跟ConfigurationManager相關的程式碼修改成如下(以connectionstring為例):

if (RoleEnvironment.IsAvailable)

{

param = RoleEnvironment.GetConfigurationSettingValue(“key”);

}

else

{

param = ConfigurationManager.ConnectionStrings[“key”].ConnectionString;

}

封裝Azure Project

在azure project按右鍵,選封裝,系統就會把專案包裝成兩個檔案,準備好上傳至Azure Cloud Service。這時候請注意VS2010的輸出,如出現WAT##等警告,請執行對應的處理。

X64平台

Cloud Service是X64平台,所以你的WebApp及所有參考的dll都需要編譯成x64使用。

服務部署

在Azure Portal選Cloud Services,選一個instance,再選Deploy,輸入Deployment Name,package是封裝時產生的.cspkg檔,configuration是.cscfg檔。完成後請選Link Resources頁面,新增SQL Database一節時所建立的 Instance。到這裡基本上已完成WebApp的部署

Appendix

Membership

如果你的程式有使用Asp.Net Memebership,請使用以下script在SQL Database的管理介面執行,這些script會建立所需要的tables, views, store procedures。

注意,必需先執行InstallCommon.sql。

網頁亂碼

請檢查WebApp的source code,需使用UTF-8儲存,檢查及修改在VS2010->檔案->進階儲存選項…

Advertisements

ASP.NET Membership/Role Model + Sitemap Security Trimming

The scenario is like this, I want to extend the ability of ASP Membership and role mode, also want to display content depends on user role.

So, I am going to use a SQL server to store and be able to extend. You can even implement you own provider class.

I will focus on how to setup and enable these features in this article.

We start with building tables in our database which we store our membership and role information.

Look under %WINDOWS%\Microsoft.NET\Framework\v2.0.50727\

There should be a ASPNET_REGSQL.exe, use it to create tables in your database.

ASPNET_REGSQL -S database_host -U username -P password -d database_name -A support_providers

for example : ASPNET_REGSQL -S 192.168.1.2 -U sa -P 1234 -d membershipDb -A All

And then, we need to configure the web.config to tell the web application that we are using membership and role.

Also, tell the provider where to get those information.

Setup a connection string to the database.

<connectionStrings>
 <remove name="LocalSqlServer"/>
 <add name="LocalSqlServer" connectionString="data source=.;Initial Catalog=MemRoleProvider;user id=sa;password=1234" providerName="System.Data.SqlClient" />
 </connectionStrings>

The [remove] delete the connect string whose name is LocalSqlServer. This seems to be the default connection string name.

You may use your own connection string name and do not remove the default one.

At last, we create the provider for the application in the [system.web] section.

<membership defaultProvider="AnaSystemSqlMembershipProvider" userIsOnlineTimeWindow="15">
 <providers>
 <clear/>
 <add name="AnaSystemSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider,System.Web, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="LocalSqlServer" enablePasswordRetrieval="false"
enablePasswordReset="true" requiresQuestionAndAnswer="true" applicationName="/" requiresUniqueEmail="false"
passwordFormat="Hashed" maxInvalidPasswordAttempts="5" passwordAttemptWindow="10"/>
 </providers>
 </membership>

 <roleManager enabled="true" defaultProvider="AnaSystemSqlRoleProvider">
 <providers>
 <add name="AnaSystemSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="LocalSqlServer" applicationName="/"/>
 </providers>
 </roleManager>

The [clear] remove all existing provider. The attributes reference can be found here.

ConnectionStringName should be the one that you previously configure. You should now able to leverage the power of ASP membership and role model.

The next step is to integrate with Security Trimming. I will use this feature to populate navigation menu depending on user role.

You have to have a  sitemap in your web application, then activeate the security trimming like this.

Inside [system.web].

<siteMap enabled="true" defaultProvider="XmlSiteMapProvider">
 <providers>
 <clear/>
 <add name="XmlSiteMapProvider"
 description="Default SiteMap provider."
 type="System.Web.XmlSiteMapProvider"
 siteMapFile="Web.sitemap"
 securityTrimmingEnabled="true" />
 </providers>
 </siteMap>

[clear] is to remove existing sitemap provider. Next we have to enable the authenticatin feature.

Inside [system.web]

<authentication mode="Forms">
 <forms name=".mycookie" path="/" loginUrl="Default.aspx" protection="All" timeout="90"/>
 </authentication>

The name attribute inside forms tag is the unique cookie name for the application.

Reference for other attribute can be found here.

The next step is to setup access rule for your directories. ASP access rule is directory base.

The root directory access rule is inside project’s web.config. Each directory will also have its

own web.config. Here is an example to give you an idea. inside [system.web]

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

This means the root directory is accessable by admin role and forbid anonymous users.

You can manage access rule with a UI, in VS2008->Project->ASP.NET administration tools->security.

The final step is to add some setting to your sitemap file, web.sitemap.

Add roles=”*” to your siteMapNode to enable security trimming base on access rules.

Here is an example.

<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
 <siteMapNode title="Home" description="Home"
 url="~/default.aspx" >

 <siteMapNode title="Guest Area" roles="*">
 <siteMapNode url="~/Guest/Guest.aspx" title="guest only"  description="" />
 </siteMapNode>
 <siteMapNode title="Power Area" roles="*">
 <siteMapNode url="~/PowerUser/Content.aspx" title="power user"  description="" />
 </siteMapNode>
 </siteMapNode>

</siteMap>

That’s it. You can also fine tune it to access rule base on page file by that is beyond the scope of this article.

You can find a little reference about that here.

中文版在這裡