How to Implement ASP.Net Membership in MVC Project

2020-03-03 06:42发布

I am new in ASP.NET Membership. And I want to implement ASP.NET Membership in ASP.NET MVC project. I am not sure is it possible or not.

Whenever we going to create MVC application at that time we found option for change authentication(no authentication, identity authentication, organization etc.). I've select identity authentication and get those tables in my application. look attached.

enter image description here

But I don't want ASP.NET Identity table and functionality.

I want ASP.NET Membership table and functionality(user CRUD operations, assign role, change user password etc) look attached.

enter image description here

I have found the way for create ASP.NET Membership table in SQL Server.

  1. Go to directory C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319 Click
  2. aspnet_regsql.exe. You will see the following screen.

But do you remember whenever we create MVC application with identity then they provide registration and login functionality inbuilt(no need to write code for that) yeah same, I want in ASP.NET Membership too.

Your answer will appreciable! :)

Thanks in advance :)

3条回答
迷人小祖宗
2楼-- · 2020-03-03 07:06

If you really want to use ASP.NET membership, the simplest way is to use ASP.NET built-in functionality, because you can use old ASP:NET as part of ASP.NET MVC application.

Another approach is to write these administration pages by yourself. It means to write controller and views. There are more pitfalls with this approach.

Transaction pitfall: If you want to combine the registration with another read/write action to another part of your database, you would like probably use transaction to keep database consistent. But you cannot combine ASP.NET membership function with another code (ADO.NET, Linq-2-SQL or Entity Framework) because database transaction need to be proceed in one database connection. Build in ASP.NET Membership function use custom connection provided by connection string in web.config. Thus transaction is performed on two connections (ASP.NET Membership connection and your connection) and it does work unless you configure your server for distributed transactions. This is not sometime supported by hosting providers.

Finally I have solved this by rewriting the membership providers myself from scratch calling the ASP.NET membership procedures. It solved the problems with two connection and distributed transaction.

Entity Framework pitfall: It is possible that you are going to use Entity Framework in you application. You will probably need to ask for membership in some of your business layer. EntityFramework does not support IDBConnection or DBConnection, but you need to provide EntityConnection. It means Entity Framework use different type of connection that ADO.NET. And calling ASP.NET membership stored procedures from entity framework does not provide you the return code. I solved this issue by creating wrapper in SQL server for every ASP.NET membership stored procedure.

So finally integrating ASP.NET membership into modern ASP.NET MVC application with Entity Framework is really a lot of work. I wouldn't do that for new project. For new project try to use OWIN instead.

查看更多
再贱就再见
3楼-- · 2020-03-03 07:14

Steps 1.Registering Database in .C:\Windows\Microsoft.NET\Framework64\v4.0.30319 aspnet_regsql

2.Add membership default provider in web.config

3.Create register and login model and their corresponding view

4.AccountController

Using Membership.CreateUser(string Username,string Password,string Email) to create new user.

using Membership.validateUser(string username , string password) to validate the entered credentials.

5.On Successful logging in Create Authentication Cookie
create ticket ->using FormsAuthenticationTicket()
encrypt the ticket
create new cookie
add this cookie in Response.Cokies

查看更多
狗以群分
4楼-- · 2020-03-03 07:26

I am new in ASP.NET Membership.

ASP.Net Membership you mentioned is more than 10 years old. Microsoft has deprecated it. Here is the history -

  1. ASP.Net Membershi Provider (you mentioned)
  2. ASP.NET Universal Providers
  3. Simple Membership Provider
  4. ASP.NET Identity (current version is 2)

Microsoft is moving toward ASP.Net Identity. It is why you cannot find any article regarding MVC and old Membership.

Since you are also new to ASP.Net Membership, I personally recommend to learn ASP.Net Identity. Here is the free course and book -

ASP.NET MVC 5 Fundamentals by Scott Allen

ASP.NET identity from Adam Freeman's book

How to Implement ASP.Net Membership in MVC Project?

You can use AccountController generated by ASP.Net Identity, and replace UserManager with Membership by yourself.

查看更多
登录 后发表回答