如何在MVC 4定制WebSecurity.Login和WebSecurity.CreateUserAndAccount方法呢? [英] How to create custom WebSecurity.Login and WebSecurity.CreateUserAndAccount methods in MVC 4?

查看:1490
本文介绍了如何在MVC 4定制WebSecurity.Login和WebSecurity.CreateUserAndAccount方法呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们选择新建项目 - > MVC 4 - >互联网应用程序,它就会自动生成的AccountController我们。在此控制器,我只在乎2的动作,登录注册

When we choose New Project --> MVC 4 --> Internet Application, it will automatically generate AccountController for us. In this controller, I only care about 2 actions, Login and Register.

在MVC 3,它采用会员的静态方法,的ValidateUser 登录动作和 CREATEUSER 注册。所以,如果我想这与我自己的数据库,我只需要通过扩展创建 CustomMembershipProvider 的MembershipProvider 和覆盖集成了两种方法

In MVC 3, it uses Membership's static methods, ValidateUserin Login action and CreateUser in Register. So, if I want to integrate it with my own database, I just need create CustomMembershipProvider by extending MembershipProvider and override that two methods.

但在MVC 4,它使用 WebSecurity.Login WebSecurity.CreateUserAndAccount 。我的问题是:

But in MVC 4, it uses WebSecurity.Login and WebSecurity.CreateUserAndAccount. My questions are:


  1. 我怎样才能使它用我自己的数据库就像我在MVC 3做?

  2. 有什么不同?为什么它不继续使用静态成员的方法呢?为什么它要改变到 WebSecurity

  1. How can I make it use my own database like I did in MVC 3?
  2. What's the different? Why doesn't it keep using static Membership methods? Why does it have to change to WebSecurity?

非常感谢您的帮助。

推荐答案

您将需要实现你自己的成员和角色提供者。

You will need to implement you own Membership and Role providers.

自定义提供:

namespace MyApp.Helpers
{
    public class CustomProviderProvider : SimpleMembershipProvider
    {
        public override MembershipUser GetUser(string username, bool userIsOnline)
        {
            return base.GetUser(username, userIsOnline);
        }

        public override bool ValidateUser(string username, string password)
        {
            return true; // base.ValidateUser(username, password);
        }
    }
}

您只需要重写你想拦截(即的ValidateUser)的方法。您还需要注册在web.config文件中提供:

You will just need to override the methods you want to "intercept" (ie ValidateUser). You will also need to register the provider in the web.config file:

<system.web>
    <membership defaultProvider="CustomMembershipProvider">
      <providers>
        <clear/>
        <add name="SimpleMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" />
        <add name="CustomMembershipProvider" type="MyApp.Helpers.CustomMembershipProvider, MyApp" />
      </providers>
    </membership>
</system.web>

您也应该叫InitializeSimpleMembershipAttribute

You should also have a Filter called "InitializeSimpleMembershipAttribute"

namespace CustomPortal.Filters
{
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
    public sealed class InitializeSimpleMembershipAttribute : ActionFilterAttribute
    {
        private static SimpleMembershipInitializer _initializer;
        private static object _initializerLock = new object();
        private static bool _isInitialized;

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            // Ensure ASP.NET Simple Membership is initialized only once per app start
            LazyInitializer.EnsureInitialized(ref _initializer, ref _isInitialized, ref _initializerLock);
        }

        private class SimpleMembershipInitializer
        {
            public SimpleMembershipInitializer()
            {
                Database.SetInitializer<CustomPortalContext>(null);

                try
                {
                    using (var context = new CustomPortalContext())
                    {
                        if (!context.Database.Exists())
                        {
                            // Create the SimpleMembership database without Entity Framework migration schema
                            ((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
                        }
                    }
//Here is where you give it your connection string name
                    WebSecurity.InitializeDatabaseConnection("CustomPortal", "UserProfile", "UserId", "EmailAddress", autoCreateTables: true);
                }
                catch (Exception ex)
                {
                    throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex);
                }
            }
        }
    }
}

我希望这有助于...

I hope this helps...

这篇关于如何在MVC 4定制WebSecurity.Login和WebSecurity.CreateUserAndAccount方法呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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