Implementing custom login for ASP.NET MVC

我怕爱的太早我们不能终老 提交于 2019-12-05 14:26:21
tuanvt

I had the same problem since I want to implement the asp.net membership with my custom built user database, my solution was to override the default aspnet membership provider for manage users logging and aspnet role provider to manage users role. For example, my custom membership provider will look sth like this

namespace Entities
{
    public class VEMembership : MembershipProvider
    {
        public override bool ValidateUser(string username, string password)
        {
            // your logic to validate user
            // return false if not qualified, other wise true
        }
    }
}

And then in your web config file, you just need to add your default membership provider to :

<membership defaultProvider="VEMembership">
    <providers>
        <clear/>
        <add name="VEMembership"
            type="Entities.VEMembership" 
            enablePasswordReset="true"
            requiresQuestionAndAnswer="false"
            requiresUniqueEmail="false"
            passwordFormat="Hashed"
            maxInvalidPasswordAttempts="5"
            minRequiredPasswordLength="6"
            minRequiredNonalphanumericCharacters="0"
            passwordAttemptWindow="10"
            passwordStrengthRegularExpression=""
            connectionString="VEConnectionString"/>
    </providers>
</membership>

There are plenty article on wrting custom aspnet membership provider on google search, if you need any help on doing this. Hope this helps

Greatran

complete answer to ur problem can be found here

The page's User object has an IsAutheticated (User.Identity.IsAuthenticated) property that will tell you whether a user has authenticated or not. This coupled with conditional statements to show or hide data/controls (or the ASP .Net LoginView control) should allow you to do what you want. In addition, you can use ASP .Net's role provider (or roll your own custom provider) to further define what your users can access/do based on role(s) that you assign to them.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!