本文介绍了如何从控制器获取asp.net的mvc应用程序的MembershipProvider的实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从控制器中获取应用程序的MembershipProvider的实例。我有一个自定义的成员资格实现,它具有描述登录用户的自定义User属性。问题的核心是我需要检索这个User对象。

I need to get an instance of the application's MembershipProvider from within a controller. I have a custom membership implementation that has a custom User property that describes the logged in user. At the heart of the issue is that I need to retrieve this User object.

我的应用程序已经完成了MembershipProvider的骨架实现,但它工作正常,正确地验证了用户尝试登录或创建新帐户。因此,我保留了ASP.NET MVC应用程序开箱即用的AccountController的默认实现。

My application has done a skeletal implementation of the MembershipProvider, but it works fine and correctly validates users that attempt to login or create a new account. Because of that, I have kept the default implementation of the AccountController that comes out of the box for an ASP .NET MVC application.

如果你需要更多的细节,我会乐意为他们提供。

If you need anymore details, I would be happy to provide them.

推荐答案

如果在Visual Studio中创建一个新的ASP.NET MVC项目,它将自动创建一个名为 AccountController 为您。这个控制器提供了如何在ASP.NET MVC中访问MembershipProvider的例子。

If you create a new ASP.NET MVC project in Visual Studio, it will automatically create a controller called AccountController for you. This controller provides an example of how you are supposed to access the MembershipProvider in ASP.NET MVC.

简而言之,它通过隐藏来促进依赖注入(DI)和松散耦合code> IMembershipService 界面下的MembershipProvider。

In short, it promotes Dependency Injection (DI) and loose coupling by hiding the MembershipProvider behind the IMembershipService interface.

AccountMembershipService IMembershipService 实现为, MembershipProvider

您可以注入一个 IMembershipService 进入任何需要它的控制器,就像 AccountController 一样。

You can inject an instance of IMembershipService into any controller that needs it, just like the AccountController does.

这篇关于如何从控制器获取asp.net的mvc应用程序的MembershipProvider的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 07:17