开发者

Inheriting from the MembershipUser class in EF

I am using the built in .net authentication in my project and 开发者_运维百科am creating class entities in entity framework 4.0.

My classes are a Student, University and Company class. My question is can I inherit the System.Web.Security.MembershipUser class for these classes? The ef class already inherit the EntityObject class so I don't know how to do it?

thanks


Your question is a little lacking in detail but I have a go at answering.

To start you won't want to inherit from the MembershipUser class. You can (because its abstract) but you would only inherit from it if you were going to use your own logic and db tables to store the validation fields.

You wouldn't put Membership onto a University or Company class but on the members of those Entities e.g. a Student is a member of a University, as there are Employees to a Company.

Neither of the Student or Employee would have Membership information on them (within their db tables) but would more likely have information relating directly to them (e.g. an Employee has a Payroll number)

Student and Employee are both of type Person (e.g they both have Name, Address, etc.)

A Student or an Employee maybe a User of your Application (web site, desktop, etc.) but then they maybe not also, or they may be deactivated as a User but you want to keep their Person and Student/Employee information for your records.

A User will have the usual info like... UserId, Username and Password, etc. but you don't want to use this entity if you were going to put a DisplayName on the App. For security reasons, I also do not put Session information onto the User but use the UserId (encrypted MD5 one way encryption) and SessionId to validate my users Session, this means that if some how I did have a breach of security (SQL injection) the hacker would not be able to access Usernames and Password details. Anyway, I digress.

So, what is the MembershipUser class? It gets used mainly in your Authentication object which will have methods like... Login, ChangePassword, Logout, etc. These methods would call

if (!Membership.ValidateUser('username', 'password')) return false; etc.

If you wanted to use your own db tables and entityframework calls for the membership then you would look to implement a Custom Membership Provider, overriding all the standard methods and put you entity DAO requests (hopefully using Design Patterns e.g. Repositories etc.). This can be risky and difficult, but with experience not too big a deal.

Otherwise, just stick with the standard Membership (User) Provider, stick what it needs in the web.config file and have it deal with the security and you can get on with developing you Application...

a good book is Pro Asp.Net MVC Framework ... it goes through everything including unit-testing, design patterns, etc. Also a great way to spend some money is on the GOF (Gang of Four) design patterns source code... get the full works, best £60 spend ever.

I hope this helped.

Hugh


There is no EF provider, but you can write your own provider. Write a mapper class for your MembershipUser derived class. I found a nice example over here: efmembership.codeplex.com

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜