“您的登录尝试未成功”请再试一次。实现自定义成员身份时出错 [英] "Your login attempt was not successful" Please try again. error while implementing custom membership

查看:88
本文介绍了“您的登录尝试未成功”请再试一次。实现自定义成员身份时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我的申请要求是单一申请&每个客户的多个数据库。所以我实现了自定义成员资格来实现这个首先我检查用户属于哪个客户端根据该客户端的db的连接字符串我动态设置连接字符串&验证用户。



这是我的网页配置:



Hello Everyone,

My requirement of application is single application & multiple databases for each client. So I implemented custom membership to achieve this. First I check user belongs to which Client the according to connection string of that client's db I dynamically set connection string & validate user.

Here is my web config :

<membership defaultProvider="EPMembershipProvider" userIsOnlineTimeWindow="20">
      <providers>
        <clear />       
        <add name="EPMembershipProvider" type="EfficientPeople.GlobalClasses.EPMembershipProvider" connectionStringName="DBEntitiesConnectionString" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="25" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" passwordStrengthRegularExpression="" applicationName="/" />
<!--<add name="EPMembershipProvider" type="EfficientPeople.GlobalClasses.EPMembershipProvider, EfficientPeople"  connectionStringName="DBEntitiesConnectionString" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="25" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" passwordStrengthRegularExpression="" applicationName="/" />-->
      </providers>
    </membership>

    <profile>
      <providers>
        <clear />
        <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="DBEntitiesConnectionString" applicationName="/" />
      </providers>
    </profile>
    <roleManager enabled="true">
      <providers>
        <clear />
        <add connectionStringName="DBEntitiesConnectionString" applicationName="/" name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" />
        <add applicationName="/" name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" />
      </providers>
    </roleManager>





我的CustomMembership类:





My CustomMembership Class :

public class EPMembershipProvider : SqlMembershipProvider
{
    public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
    {
        base.Initialize(name, config);
        // Update the private connection string field in the base class.

        Int64 ClientId = Convert.ToInt64(HttpContext.Current.Session["_clientId"]);
        if (ClientId > 0)
        {
            UserLoginML objUL = new UserLoginML();

            //string _dbMembershipConnStr = ManageSessionFacade._dbMembershipConnStr;
            string _dbMembershipConnStr = HttpContext.Current.Session["_dbMembershipConnStr"].ToString();
            //string connectionString = ManageSessionVariable.Current._dbContextConnStr;

            if (_dbMembershipConnStr != "" && _dbMembershipConnStr != "db_not_validate" && _dbMembershipConnStr != "db_not_exist")
            {
                try
                {
                    // Set private property of Membership provider.
                    //FieldInfo connectionStringField = GetType().BaseType.GetField("_sqlConnectionString", BindingFlags.Instance | BindingFlags.NonPublic);
                    //connectionStringField.SetValue(this, _dbMembershipConnStr);
                    FieldInfo connectionStringField = GetType().BaseType.GetField("_sqlConnectionString", BindingFlags.Instance | BindingFlags.NonPublic);
                    if (connectionStringField != null)
                        connectionStringField.SetValue(this, _dbMembershipConnStr);

                    //Set role provider
                    var roleField = Roles.Provider.GetType().GetField("_sqlConnectionString", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
                    if (roleField != null)
                        roleField.SetValue(Roles.Provider, _dbMembershipConnStr);

                    //Set Profile Provider

                    var profileField = ProfileManager.Provider.GetType().GetField("_sqlConnectionString", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
                    if (profileField != null)
                        profileField.SetValue(ProfileManager.Provider, _dbMembershipConnStr);
                }
                catch (Exception ex)
                {
                    throw ex;
                }

            }
        }
    }



当我在本地运行我的应用程序时;我使用客户端A [DB1] -User1登录,然后成功登录。但是当我注销并尝试使用客户端B [DB2] User2登录时,它会抛出此错误消息:



您的登录尝试不成功



当我再次运行应用程序&我反之亦然。使用客户端B [DB2] -User2登录它成功登录但是当我在同一个应用程序运行中尝试使用客户端A [DB1] -User1时,它会抛出相同的错误。但我可以使用Client B [DB2]用户登录。



这意味着如果我第一次登录时,特定客户端数据库的用户能够登录该数据库的任何用户。我曾登录其他数据库用户无法登录。



当我使用断点进行调试时,它返回false:



bool ValidateUser(字符串Username,string Passwd)。

我检查过用户没有被锁定或不活动。会员使用相关的连接字符串作为用户。



如果有人从这个奇怪的问题出发帮助我完成。


When I runs my application locally &I login with Client A[DB1]-User1 then it logins successfully. But when I logout and trying to login with Client B [DB2] User2 it throws this error message :

Your login attempt was not successful

When I again run the application & I do vice versa ie. login with Client B[DB2]-User2 it login successfully but when I try with Client A[DB1]-User1 in the same application run it throws the same error. But I am able to login with Client B[DB2] users.

It means the users of particular client's db are able to login if very first time I logs in with any user of that db. And I once logged in other db users are not able to login.

When I debugged with breakpoints it returns false in :

bool ValidateUser(string Username, string Passwd).
I have checked the users are not locked out or inactive. And Membership uses associated connection string as per user.

If anybody goes from this strange problem & help me through.

推荐答案

问题在于SqlMembership的Initialize方法。当用户第一次登录时,它会调用initialize方法&设置连接字符串。当另一个用户尝试登录时,它使用相同的连接字符串。这就是为什么它没有验证。如果我们尝试调用Initialize方法它会抛出异常InvalidOperationException:成员资格提供程序已经初始化。



所以我创建了自定义成员资格的对象&当我通过使用新创建的对象调用成员验证方法时,我可以设置我的连接字符串。





The problem was with SqlMembership's Initialize method. When user logs in first time it calls initialize method & set connection string. When another user tries to login it uses the same connection string. that's why its not validating. If we try to call Initialize method It throws an exception of "InvalidOperationException : membership provider already initialized."

So I created the object of my custom membership & when I call Membership validate method by using newly created object I am able to set my connection string.


EPMembershipProvider myProvider = new EPMembershipProvider();
string configPath = "~/web.config";
Configuration config =     WebConfigurationManager.OpenWebConfiguration(configPath);
MembershipSection section = (MembershipSection)config.GetSection("system.web/membership");
ProviderSettingsCollection settings = section.Providers;
NameValueCollection membershipParams = settings[section.DefaultProvider].Parameters;
myProvider.Initialize("EPMembershipProvider", membershipParams);

bool Status = myProvider.ValidateUser(UserName, Password);





它运作成功。 Thanx to Shri n all ...: - )



And it worked successfully. Thanx to Shri n all... :-)


这篇关于“您的登录尝试未成功”请再试一次。实现自定义成员身份时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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