MVC 5 IOC和验证 [英] MVC 5 IoC and Authentication

查看:107
本文介绍了MVC 5 IOC和验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是即将开始的一个项目,在那里我将使用MVC5。但是,当我想用​​国际奥委会和之后重用我的用户表,并添加自定义的东西给它,我发现它很难看我怎么可以使用随MVC5来到新标识框架。

I am just about to start on a project, where I will be using MVC5. But as I want to use IoC and later reuse my user tables, and add custom stuff to it, I am finding it very hard to see how I can use the new Identity framework that came with MVC5.

我越来越看往基本形式权威性。你有什么解决方案?

I am more and more looking towards basic forms auth. What are your solutions?

我的需求:


  • 用户资料库/服务必须注射

  • 用户库必须驻留在DAL

  • 用户库必须能够支持其他技术比EF

  • 与OpenID和OAuth认证必须有所易于实施

  • 必须是安全

  • 应该是在其他项目中,例如可重复使用。 WPF

我一直在寻找了很久的答案,但我看到的一切是在控制器硬codeD。

I have been looking for a long time for an answer, but everything I see is hardcoded in the controller.

你是如何解决的?你是从头开始编写最多的,还是可以绑定到的东西会扩展到其他.NET平台的WCF和WPF?

How are you solving this? Are you writing most from scratch, or can you bind into something that will scale to other .NET platforms as WCF and WPF?

下面code是直接从的AccountController在默认的ASP.NET MVC 5模板服用。
它做的第一件事是庶子注射

The below code is taken directly from the AccountController in the default ASP.NET MVC 5 Template. The first thing it does is a Bastard Injection.

[Authorize]
public class AccountController : Controller
{
    public AccountController()
        : this(
            new UserManager<ApplicationUser>(
                new UserStore<ApplicationUser>(
                    new ApplicationDbContext())))
    {
    }

    public AccountController(UserManager<ApplicationUser> userManager)
    {
        UserManager = userManager;
    }
}

接受的答案会去的人,那表明我自己所做的事,它合并了上述要求

推荐答案

我最终决定实施IUserStore,IUserStore,IUserPasswordStore,IUserLoginStore,要能够向下移动UserRepository到它应有的位置,在数据访问层。但仍然获得Owin的安全性,并带来了好处新标识框架。

I ended up deciding to implement the IUserStore, IUserStore, IUserPasswordStore, IUserLoginStore, to be able to move the UserRepository down into it's rightful place, the DataAccess Layer. But still get the Security Benifits of the Owin and new Identity Framework.

这是很容易实现的,并没有花费太多抽象的。这里是UserStoreWrapper

It's quite easy to implement, and doesn't take much to abstract it. Here is a taste of the UserStoreWrapper

namespace qubis.booking.WebApp.App_Code.Identity
{
    public class UserServiceWrapper : IUserStore<ApplicationUserWrapper>, 
                                      IUserPasswordStore<ApplicationUserWrapper>, 
                                      IUserLoginStore<ApplicationUserWrapper>
    {
        public IUserRepository UserRepos { get; set; } // My own Interface.
        public UserServiceWrapper(IUserRepository userRepo)
        {
            UserRepos = userRepo;
        }


        public async Task CreateAsync(ApplicationUserWrapper user)
        {
            UserRepos.Insert(user.RealUser);
        }

        public async Task<ApplicationUserWrapper> FindByIdAsync(string userId)
        {
            var appUser = UserRepos.FindByUserName(userId);
            ApplicationUserWrapper wrappedUser;
            if (appUser != null)
            {
                wrappedUser = new ApplicationUserWrapper(appUser);
            }
            else
                wrappedUser = null;
            return wrappedUser;
        }

在帐户控制我只是简单地要求它被注入:

In the Account controller I Simply just ask for it to be injected:

public AccountController(UserManager<ApplicationUserWrapper> userManager)
{
    UserManager = userManager;{ AllowOnlyAlphanumericUserNames = false };
}

和为我使用Ninject我只是把它UPIN内核像这样:

And as I am using Ninject I just set it upin the kernel like so:

// <summary>
// Load your modules or register your services here!
// </summary>
// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
    kernel.Bind<IUserStore<ApplicationUserWrapper>>().To<UserServiceWrapper>();
    kernel.Bind<UserManager<ApplicationUserWrapper>>().ToSelf();
}

要看到标识框架结构,请参阅此文章。 <一href=\"http://www.asp.net/identity/overview/extensibility/implementing-a-custom-mysql-aspnet-identity-storage-provider\" rel=\"nofollow\">http://www.asp.net/identity/overview/extensibility/implementing-a-custom-mysql-aspnet-identity-storage-provider

To see the Identity frameworks structure, please see this article. http://www.asp.net/identity/overview/extensibility/implementing-a-custom-mysql-aspnet-identity-storage-provider

这篇关于MVC 5 IOC和验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆