terça-feira, 23 de setembro de 2008

Ainda para o MEMBERHIP

Em adição ao artigo anterior fiz tb o que sugerem noutro artigo:

Membership/Roles with Remote DB
(Other than SQL Express)

This will be a very short tutorial, actually. If you know how easy it is to create all the tables, and stored procedures with the ASP.Net Login controls, you know that it creates a database, by default, in the App_Data directory, called ASPNet.mdf. However, in the real world, that sometimes doesn't cut it - especially if you have a hosted web site.

There are only two steps to get everything setup on any other SQL 2000 or SQL 2005 server. The first step is to use the ASPNet_regsql.exe application that comes with your installation. The hardest part about using this is that you will need to use the Command Prompt (Start/Run/cmd) and navigate to the correct directory/folder, which is:

C:\windows\Microsoft.net\Framework\
At the time of this writing, the version is v2.0.50727. When you get to the directory, type in:
aspnet_regsql -W
There are a lot of other registration options that are possible, but this will get you to the GUI to handle this, instead of having to learn what all the options might or might not mean to you. The rest is no more difficult than entering the IP address of your SQL Server, the user ID and the Password. Just follow the wizard all the way through this, and it will setup all the Tables and Stored Procedures you need to handle this.

The last and second step is to create a ConnectionString entry in the ConnectionString section of your web.config for your application, using 'LocalSQLServer' as the name:



connectionString="server=YourSrvr;uid=YourUID;pwd=YourPWD;database=YourDB"
providerName="System.Data.SqlClient" />
As you probably noticed, there is a line above the connectionString, with 'remove LocalSQLServer'. If you already have created a connection to the default server, this may be in the Machine.config file already. This makes sure the one you want is used. It removes it from memory, if it's there, and then adds your particular server back.

Then, the last step is to add a Membership/Provider section to the Web.Config to tie it all together:




enablePasswordRetrieval="false" enablePasswordReset="true"
applicationName="/" requiresUniqueEmail="false" passwordFormat="Hashed"
maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6"
minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
passwordStrengthRegularExpression="" requiresQuestionAndAnswer="true"
type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />

Here, the default name, 'AspNetSqlMembershipProvider' is used, adding it as the defaultProvider in the Membership section and assigning (the name attribute) in the Provider section. Also notice here, that there are a lot of attributes you can use to tighten down the security in your application.

Remember, at the first of this tutorial, when I said it would be very short and easy? Well, that's all there is to it.

Sem comentários: