Service Provider Membership Provider

For practical purposes, I'm going to create a new ASP.NET MVC 3.0 application. My solution (Practice.sln) will have 4 layers:

  • Pratice.Common (class library for my ViewModels)
  • Pratice.Data (class library for EF)
  • Pratice.Service (class library for business logic)
  • Pratice.Web (asp.net mvc 3.0 project)

Suppose I have a view called "Login" that is strongly typed on the LoginModel defined at my Practice.Common level. LoginModel has 2 properties (username and password).

In my controller, when the user submits the form, I call the following method:

[HttpPost] public ActionResult Login(LoginModel model) { if(_service.ValidateUser(model)) return null; } 

ValidateUser () is a method defined at my Pratice.Service level (inside my LoginService.cs file).

Im basically delegating the verification process to my service level ...


My question is this:

Given that Id like to use / take advantage of the membership provider and considering that most (if not all) of my logic happens at my service level, how can I transfer membership to my service level? (if it's even a good thing)

Also ... I planned to create my own membership provider, not built-in, since Im did not use all these generating TABLES and sprocs ...

Bonus question:

Is it considered best practice that all login and account management come directly from your controller and the rest of my business logic is stored inside my service level?

I am curious about the pros and cons of having β€œparts” of logic that occur directly inside the Controller and other β€œparts” that occur at the service level.

Of course, if someone has a link or article that explains this, I will be grateful!

Yours faithfully

+4
source share
1 answer

Well, after several tests and more reading, Ive managed to answer my own questions.

Regarding the transfer of the membership provider to my service level (in my case), which does not make any sense, since my service level will now depend on System.Web.Security, and I do not want this.

In addition, I quickly realized that I was confusing the two concepts. FormsAuthentication and membership. Although they work hand in hand, I do not need all the methods provided by membership. Therefore, I do not need to create a custom membership provider or use the built-in.

All I have to do is continue to create my methods at my service level (e.g. Login () method), and then manually create the FormsAuthenticationTicket, which I will add inside the cookie, and then add this cookie to the cookie collection ( in the controller).

As an additional note, I also realized that only after you have added a cookie to the cookie collection, HttpContext.User.Identity.IsAuthenticated will start to return TRUE.

As for my bonus question, unless stated otherwise, I will keep the login mechanism (and check) at the service level instead of having some logic in the controller and some logic at the service level.

+3
source

Source: https://habr.com/ru/post/1381202/


All Articles