Posts

Showing posts with the label ASP.NET Membership

Donate

Seed Roles And Users To An Existing Database In ASP.NET MVC 5 Using Identity

Image
Hello, In this tutorial, I will demonstrate seeding roles and users to an existing database in an ASP.NET MVC 5 application using Identity framework. For simplicity, I will use a Northwind. This database doesn't have membership tables at all. To begin with, accomplish the steps below: 1. Create an ASP.NET MVC application and then change web.config connectionStrings element to connect to an existing database (Northwind as my example). 2. Under Package Manager Console type PM> Enable-Migrations 3. Under Package Manager Console type PM> Add-Migration ASPMembership * This will create a file Timespan_ASPMembership.cs inside Migrations folder with scripts to create Membership Tables such as AspNetUsers, AspNetRoles and etc. 4. Under Package Manager Console type PM> Update-Database * This will add membership tables to Northwind database. 5. To seed Roles and Users, create a class SeedRolesAndUsers.cs inside Models folder. This class referenc...

ASP.NET MVC 4 Built-in Form Based Authentication

Image
Given that I have some background knowledge of ASP.NET Web Forms Authentication, I decided to try experimenting on MVC 4 authentication/authorization framework. Most of the articles in the internet regarding forms authentication in ASP.NET MVC are custom based approach and less on the built-in technology which is WebData.WebSecurity usage. Not until I found these articles: a. Forms Authentication Customized b. Introduction to forms based authentication in MVC 4 c. Authenticating Users In Asp.net MVC 4 So, to cut the story short, I made an application which utilized the built-in WebMatrix authentication in MVC 4. The first thing to do, is to familiarize those articles to get a good grasp on the topic. The setup of my application are as follows: 1. Web.config      1.1 Added authentication and custom errors <authentication mode= "Forms" > <forms loginUrl= "~/Account/Login" timeout= "2880" /> </...

Redirect Unauthorized Access To A Custom View Instead Of Redirecting To A Login View In ASP.NET MVC 4

One might encounter when implementing the forms authentication framework(WebMatrix)is that when a user access a specific url/controller and he/she is unauthorized, the application always redirect's to the default log-in view. In order to solve this minor issue, one solution is to develop a custom class that inherit's the AuthorizeAttribute class and override the HandleUnauthorizedRequest method as shown below: public class AuthorizeUsersAttribute : AuthorizeAttribute { private string redirectUrl = "" ; public string NotifyUrl { get { return redirectUrl; } set { redirectUrl = value ; } } public AuthorizeUsersAttribute() : base () { } public AuthorizeUsersAttribute( string redirectUrl) : base () { this .redirectUrl = redirectUrl; } protected override void HandleUnauthorizedRequest(Authorizati...

You must call the "WebSecurity.InitializeDatabaseConnection" method before you call any other method of the "WebSecurity" class

In the Account Controller Register action (public ActionResult Register(RegisterModel model)) I have a code below that by default assign a Customer Role to a new user during registration. In my database, the defined roles are "Administrator" and "Customer". However, the code below generates exception as stated on the title post. SimpleRoleProvider role = new SimpleRoleProvider(); role.AddUsersToRoles( new string [] { model.UserName }, new string [] { "Customer" }); The solution would be to use the Role class defined in System.Web.Security namespace instead of SimpleRoleProvider: Roles.AddUserToRole(model.UserName, "Customer" ); Reference: Perils of MVC 4 AccountController Cheers! :)

Deploying ASP.NET Web Forms Websites Using ASP.NET Membership Framework

In my case, i have been experimenting an e-commerce application which uses asp.net membership framework. If I run the website using visual studio 2008, it successfully logs in using the users i have created using the built-in asp.net website administration tool. However, if you deploy this on a production server such as IIS, you cant' log-in. The solution is straightforward in this link: Always set the "applicationName" property when configuring ASP.NET 2.0 Membership and other Providers(Configuring ASP.NET 2.0 Membership)

Donate