asp.net-mvc-4 – 如何在MVC 4中创建自定义WebSecurity.Login和WebSecurity.CreateUserAndAccount方法?

前端之家收集整理的这篇文章主要介绍了asp.net-mvc-4 – 如何在MVC 4中创建自定义WebSecurity.Login和WebSecurity.CreateUserAndAccount方法?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当我们选择New Project – > MVC 4 – >互联网应用,它将自动为我们生成AccountController.在这个控制器中,我只关心2个操作,登录注册.

在MVC 3中,它使用Membership的静态方法,ValidateUserin Login操作和CreateUser在Register中.所以,如果我想将它与我自己的数据库集成,我只需要创建CustomMembershipProvider通过扩展MembershipProvider并覆盖这两个方法.

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

>如何使用我自己的数据库,就像我在MVC 3中一样?
>有什么不同?为什么不继续使用静态会员方法?为什么要改变为WebSecurity?

非常感谢你的帮助.

解决方法

您将需要实现您自己的会员和角色提供商.

自定义提供者

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文件注册提供程序:

<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”的过滤器

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);
                }
            }
        }
    }
}

我希望这有帮助…

猜你在找的asp.Net相关文章