Showing posts with label Membership. Show all posts
Showing posts with label Membership. Show all posts

Tuesday, April 6, 2010

Asp .Net Membership -III aspnet_regsql command tool, configuring the Site to using Instance Database and Working with Membership and Roles Api

Membership and Role Api Methods


In the blog Asp .Net Membership - II aspnet_regsql to port Membership API database to Sql Server we saw aspnet_regsql tool.


The tool can also be invoked from command prompt, which provides fine grained support on which tables you want to dump to sql server Instance database.

If you want to dump just Membership and Role tables to Sql Server you issue the command as follows:

>aspnet_regsql -S . -E -d Shalvin -A mr


Here -S denoted the Sql Serve instance. Since I am using Sql server 2000 I am specifying a dot. -E denoted integrated security. Optionally you can specify user name and password if you are using Sql Server Security. -d denoted the database name. -A is used for adding the tables. Here I am adding Membership and Role tables.
Like wise you can remove tables using -R.

Next order of business it the instruct your Asp .Net project to use Instance database instead of File System Database.

For that you have edit the web.config as follows:









<configuration>
<connectionStrings>
<add name="ShalvinConnectionString" connectionString="Data Source=.\shalvin;Initial Catalog=Shalvin;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>

<roleManager enabled="true">
<providers>
<clear/>
<add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ShalvinConnectionString"/>
</providers>
</roleManager>

<authentication mode="Forms"/>

<membership>
<providers>
<clear/>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ShalvinConnectionString"/>
</providers>
</membership>
</system.web>
</configuration>



Programmatic working with Membership and Roles Api

using System.Web.Security;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ddlUsers.DataSource = Membership.GetAllUsers();
ddlRole.DataSource = Roles.GetAllRoles();

DataBind();
}
}
protected void btnAddUserToRole_Click(object sender, EventArgs e)
{
try
{
Roles.AddUserToRole(ddlUsers.Text, ddlRole.Text);
Response.Write("User Added to role");
}
catch (Exception ex)
{
Response.Write(String.Format("{0} is already an {1}",
ddlUsers.Text, ddlRole.Text));
}
}






Related Blog
Asp .Net 2.0 Membership I - LoginStatus, Login and LoginView Controls
Asp .Net Membership - II aspnet_regsql to port Membership API database to Sql Server

Tuesday, February 2, 2010

Asp .Net Membership - II aspnet_regsql to port Membership API database to Sql Server

In the blog Asp .Net Membership I - LoginStatus, Login and LoginView Controls
we saw how to work with Asp .Net Membership service.

By default the Membership Provider is creating a File System Database called AspNetDb.mdf under App_Data folder. This will suffice for small projects, where you can create rest of the tables in AspNetDb.mdf.
This by no means is an elegant solution, since usually the database will reside on Sql Server as Instance database and not in File System database. In effect there will be two database one for membership, role, profile, personalization, etc. and another for the application.

It is possible to merge the membeership database with out application database.

For that you can use aspnet_regql tool found in C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727 folder incase. Incase you are using Asp .net 4 the folder will be C:\WINDOWS\Microsoft.NET\Framework\v4.0.30128.

The wizard will prompt for the database to which you want to dump the membership, role, profile and personalization tables and associated database objects.

Related Blog

Asp .Net 2.0 Membership I - LoginStatus, Login and LoginView Controls

Tuesday, June 3, 2008

Asp .Net 2.0 Membership I - LoginStatus, Login and LoginView Controls





This is part I of a series of Membership blogs I am planning to post.
This blog is intended to the a supplement to my Youtube video Asp.Net 2.0 Membership I : LoginStatus LoginName.



Authentication and Authorization are the most important, time consuming and error prone task in a site development. Thanks to ASP .Net 2.0's membership options, the whole task is simplified.

There are there Authentication options available in Asp. Net :
1. Windows Authentication
2. Forms Authentication
3. Passport Authentication

Windows Authentication is used in small intranet sites, where the the authentication process is tied to Corporate Active directory. This is the default authentication type in ASP .Net.

Forms Authentication (Web Forms Authentication) use a Database to store user credentials and is approach used in public facing website that we encounter on a daily basis. This blog concentrates on Forms Authentication.

Passport Authentication is an authentication option provided by Microsoft, whereby Microsoft handles the whole authentication process. This option is mainly found in Microsoft family of websites.

In this blog I am going to concentrate on Forms Authentication.


Asp .Net comes packed a number of Login controls as shown in the figure below.
















Go to Website and select ASP.Net Configuration. Web site administration tool will appear.




















Select security and select authentication type, select From the Internet as the authentication type. Click Done. Now you are done with Forms Authentication.




















If you inspect the web.config the Authentication mode has changed to forms and inside the App_Data a new database ASPDBNETDB.MDF is created.

Click on Create User in Web Site Administration tool.
Create user form will appear. Here you expected to give a strong password which is more than 7 characters in length and contain at least one special character.

LoginStatus Control
Now come back to Default.aspx and place a LoginStatus control. LoginStatus Control acts as a toggle control control and redirects you to the login page.



















Create a new page called Login.aspx. Place a Login Control. Now when you run the application and click on loginstatus control you will be taken to the login page. On successful submission of user name and password you will be redirected back to the default page. Other wise you can't proceed as show in the figure.



















LoginName Control

All the sites provides on option seeing the user name. This can be accomplished using LoginName control. So place a LoginName control on default.aspx and again follow the same login process on successful login you will see the login name.

If you want a greeting message like Hello prefixed on the user name you can use the FormatString property of LoginName control like this : Hello...{0}.

LoginView Control
Using LoginView Control you can have custom contents for Anonymous and Logged In Users.
For this LoginView Control is having two templates Anonymous Template and Logged In Template.

Anonymous Template is the default template where you can specify the contents for anonymous users.




















In LoggedIn Template you can specify the contents that can be viewed by logged in users. In this example I am adding the guitar chords of Hotel California.

The following figure shows the Anonymous template in action.













The following figure shows the Logged in template in action.


















Related Blogs

aspnet_regsql to port Membership API database to Sql Server



Happy Programming
Shalvin.com