- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试在我自己的数据库中使用自定义 MemberShipUser 实现自定义 MembershipPriver(具有特定的用户表模型):
这是完全不同的文件:
iTwitterMembershipProvider.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Collections.Specialized;
using iTwitter.Models;
public class iTwitterMembershipProvider : MembershipProvider
{
public override string ApplicationName
{
get { return _ApplicationName; }
set { _ApplicationName = value; }
}
public override bool ChangePassword(string username, string oldPassword, string newPassword)
{
throw new NotImplementedException();
}
public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
{
return false;
}
public override iTwitterMembershipUser CreateUser(string login,
string password,
string email,
string tokenKey,
string tokenSecret,
string twitterUserId,
object providerUserKey,
out MembershipCreateStatus status)
{
ValidatePasswordEventArgs args = new ValidatePasswordEventArgs(login,
password,
true);
OnValidatingPassword(args);
if (args.Cancel)
{
status = MembershipCreateStatus.InvalidPassword;
return null;
}
if (RequiresUniqueEmail && GetUserNameByEmail(email) != "")
{
status = MembershipCreateStatus.DuplicateEmail;
return null;
}
iTwitterMembershipUser u = GetUser(login, false);
if (u == null)
{
UserRepository _user = new UserRepository();
_user.CreateUser(login, password, email);
status = MembershipCreateStatus.Success;
return GetUser(login, false);
}
else
{
status = MembershipCreateStatus.DuplicateUserName;
}
return null;
}
public override bool DeleteUser(string username, bool deleteAllRelatedData)
{
throw new NotImplementedException();
}
public override bool EnablePasswordReset
{
get { return _EnablePasswordReset; }
}
public override bool EnablePasswordRetrieval
{
get { return _EnablePasswordRetrieval; }
}
public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
{
throw new NotImplementedException();
}
public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
{
throw new NotImplementedException();
}
public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
{
throw new NotImplementedException();
}
public override int GetNumberOfUsersOnline()
{
throw new NotImplementedException();
}
public override string GetPassword(string username, string answer)
{
throw new NotImplementedException();
}
public override iTwitterMembershipUser GetUser(string login, bool userIsOnline)
{
UserRepository _user = new UserRepository();
return _user.GetUser(login);
}
public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
{
throw new NotImplementedException();
}
public override string GetUserNameByEmail(string email)
{
UserRepository _user = new UserRepository();
return _user.GetUserNameByEmail(email);
}
public override int MaxInvalidPasswordAttempts
{
get { return _MaxInvalidPasswordAttempts; }
}
public override int MinRequiredNonAlphanumericCharacters
{
get { return _MinRequiredNonalphanumericCharacters; }
}
public override int MinRequiredPasswordLength
{
get { return _MinRequiredPasswordLength; }
}
public override int PasswordAttemptWindow
{
get { return _PasswordAttemptWindow; }
}
public override MembershipPasswordFormat PasswordFormat
{
get { return _PasswordFormat; }
}
public override string PasswordStrengthRegularExpression
{
get { return _PasswordStrengthRegularExpression; }
}
public override bool RequiresQuestionAndAnswer
{
get { return _RequiresQuestionAndAnswer; }
}
public override bool RequiresUniqueEmail
{
get { return _RequiresUniqueEmail; }
}
public override string ResetPassword(string username, string answer)
{
throw new NotImplementedException();
}
public override bool UnlockUser(string userName)
{
throw new NotImplementedException();
}
public override void UpdateUser(MembershipUser user)
{
throw new NotImplementedException();
}
public override bool ValidateUser(string login, string password)
{
if (login == password)
{
return true;
}
else
{
return false;
}
}
//
// A helper function to retrieve config values from the configuration file.
//
private string GetConfigValue(string configValue, string defaultValue)
{
if (string.IsNullOrEmpty(configValue))
return defaultValue;
return configValue;
}
//
// Properties from web.config, default all to False
//
private string _ApplicationName;
private bool _EnablePasswordReset;
private bool _EnablePasswordRetrieval = false;
private bool _RequiresQuestionAndAnswer = false;
private bool _RequiresUniqueEmail = true;
private int _MaxInvalidPasswordAttempts;
private int _PasswordAttemptWindow;
private int _MinRequiredPasswordLength;
private int _MinRequiredNonalphanumericCharacters;
private string _PasswordStrengthRegularExpression;
private MembershipPasswordFormat _PasswordFormat = MembershipPasswordFormat.Hashed;
public override void Initialize(string name, NameValueCollection config)
{
if (config == null)
throw new ArgumentNullException("config");
if (name == null || name.Length == 0)
name = "CustomMembershipProvider";
if (String.IsNullOrEmpty(config["description"]))
{
config.Remove("description");
config.Add("description", "Custom Membership Provider");
}
base.Initialize(name, config);
_ApplicationName = GetConfigValue(config["applicationName"],
System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath);
_MaxInvalidPasswordAttempts = Convert.ToInt32(
GetConfigValue(config["maxInvalidPasswordAttempts"], "5"));
_PasswordAttemptWindow = Convert.ToInt32(
GetConfigValue(config["passwordAttemptWindow"], "10"));
_MinRequiredNonalphanumericCharacters = Convert.ToInt32(
GetConfigValue(config["minRequiredNonalphanumericCharacters"], "1"));
_MinRequiredPasswordLength = Convert.ToInt32(
GetConfigValue(config["minRequiredPasswordLength"], "6"));
_EnablePasswordReset = Convert.ToBoolean(
GetConfigValue(config["enablePasswordReset"], "true"));
_PasswordStrengthRegularExpression = Convert.ToString(
GetConfigValue(config["passwordStrengthRegularExpression"], ""));
}
}
iTwitterMembershipUser.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using iTwitter.Models;
namespace iTwitter.Models
{
public class iTwitterMembershipUser : MembershipUser
{
private string _tokenKey;
private string _tokenSecret;
private string _twitterUserId;
private string _login;
private string _email;
private int _providerUserKey;
public string tokenKey
{
get { return _tokenKey; }
set { _tokenKey = value; }
}
public string tokenSecret
{
get { return _tokenSecret; }
set { _tokenSecret = value; }
}
public string twitterUserId
{
get { return _twitterUserId; }
set { _twitterUserId = value; }
}
public string login
{
get { return _login; }
set { _login = value; }
}
public string email
{
get { return _email; }
set { _email = value; }
}
public int providerUserKey
{
get { return _providerUserKey; }
set { _providerUserKey = value; }
}
public iTwitterMembershipUser(object providername,
string login,
int providerUserKey,
string email,
string tokenKey,
string tokenSecret,
string twitterUserId)
{
this.tokenKey = tokenKey;
this.tokenSecret = tokenSecret;
this.twitterUserId = twitterUserId;
this.login = login;
this.providerUserKey = providerUserKey;
this.email = email;
}
}
}
网络配置.cs
<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=152368
-->
<configuration>
<connectionStrings>
<add name="iTwitterDB" connectionString="metadata=res://*/Models.iTwitter.csdl|res://*/Models.iTwitter.ssdl|res://*/Models.iTwitter.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\iTwitter.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" /></connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /></assemblies>
</compilation>
<authentication mode="Forms">
<forms loginUrl="~/Account/LogOn" timeout="2880" />
</authentication>
<membership defaultProvider="CustomMembershipProvider">
<providers>
<clear />
<add name="CustomMembershipProvider" type="iTwitterMembershipProvider" connectionStringName="iTwitterDB" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
</providers>
</membership>
<profile>
<providers>
<clear />
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="iTwitterDB" applicationName="/" />
</providers>
</profile>
<roleManager enabled="false">
<providers>
<clear />
<add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="iTwitterDB" applicationName="/" />
<add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
</providers>
</roleManager>
<pages>
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
</namespaces>
</pages>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
iTwitter数据库:
id int
login varchar(50)
password varchar(100) (MD5 Password Hash)
email varchar(100)
tokenKey varchar(100)
tokenSecret varchar(100)
twitterUserId varchar(100)
UserRepository.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
namespace iTwitter.Models
{
public class UserRepository
{
public iTwitterMembershipUser CreateUser(string login, string password, string email)
{
using (iTwitterDB db = new iTwitterDB())
{
Users user = new Users();
user.login = login;
user.email = email;
user.password = password;
user.tokenKey = "0";
user.tokenSecret = "0";
user.twitterUserId = "0";
db.AddToUsers(user);
db.SaveChanges();
return GetUser(login);
}
}
public string GetUserNameByEmail(string email)
{
using (iTwitterDB db = new iTwitterDB())
{
var result = from u in db.Users where (u.email == email) select u;
if (result.Count() != 0)
{
var dbuser = result.FirstOrDefault();
return dbuser.login;
}
else
{
return "";
}
}
}
public iTwitterMembershipUser GetUser(string login)
{
using (iTwitterDB db = new iTwitterDB())
{
var result = from u in db.Users where (u.login == login) select u;
if (result.Count() != 0)
{
var dbuser = result.FirstOrDefault();
string _login = dbuser.login;
int _providerUserKey = dbuser.id;
string _email = dbuser.email;
string _tokenKey = dbuser.tokenKey;
string _tokenSecret = dbuser.tokenSecret;
string _twitterUserId = dbuser.twitterUserId;
iTwitterMembershipUser user = new iTwitterMembershipUser("CustomMembershipProvider",
_login,
_providerUserKey,
_email,
_tokenKey,
_tokenSecret,
_twitterUserId);
return user;
}
else
{
return null;
}
}
}
}
}
我在编译过程中遇到了一些错误:
1) 'iTwitterMembershipProvider' does not implement inherited abstract
member 'System.Web.MembershiProvider.CreateUser(string, string, string, string, string, bool, object, out System.Web.Security.MembershipCreateStatus)'
2) iTwitterMembershipProvider.CreateUser(string,
string, string, string, string, bool, object, out System.Web.Security.MembershipCreateStatus)': no suitable method found to override
3)'iTwitterMembershipProvider.GetUser(string,
bool)': return type must be 'System.Web.Security.MembershipUser' to match overridden member 'System.Web.Security.MembershipProvider.GetUser(string, bool)'
我在这里被封锁了。
谢谢。
最佳答案
将CreateUser 和GetUser 的返回类型更改为基类MembershipUser。当您调用这些方法时,您可以将其转换为 iTwitterMembershipUser 类型。例如,在 CreateUser 中,您将返回语句更改为
return (iTwitterMembershipUser) GetUser(login);
关于c# - 实现自定义 MembershipUser 和自定义 MembershipProvider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5556031/
我想从我的页面获取当前的 MembershipUser。 Membership.GetUser(); 返回空值。 无法使用 HttpContext.Current.User.Identity.Name
是否可以在没有密码的情况下验证 MembershipUser。我有 gues 用户帐户并且有临时密码,但是我不要求这些用户实际登录。我想在他们的用户 ID 上自动对他们进行身份验证。 这可以做到吗?
我们最近将我们的解决方案升级到了 .NET 4.5。 我了解到 System.Web.Security.MembershipUser 已移至 System.Web.ApplicationService
我正在使用属于 MVC2 默认项目的 MembershipProvider。 我希望能够获取用户名列表,然后注销用户,并在需要时销毁他们的 session 。我能想到的最接近的是: foreach(s
我正在考虑在 ASP.Net MVC3 中创建自定义成员资格提供程序,并且正在努力了解它们如何组合在一起...... 我真的在寻找一些关于如何做到这一点的最佳实践方法。 我有一个用户模型(代表我数据库
我已将类库项目添加到我的应用程序中。 在我的一个类中,我需要使用 MembershipUser 类,但项目找不到它。我添加了对 System.Web、System.Web.Security 和 Sys
我目前正在开发一个 asp.net mvc 2 应用程序,它使用默认的 SqlMembershipProvider 进行身份验证。我已经实现了一个 Controller 方法,它通过调用 Member
我正在构建一个自定义 MembershipProvider,更准确地说是 GetUser 函数。 因此我有一个自定义的 MembershipUser。 public class CustomMembe
我需要更多与用户相关的字段。那么,扩展 MembershipUser 类还是创建与 MembershipUser 类 UserID 有关系(外键)的单独类是好主意吗? 最佳答案 在被视为与成员(mem
我编写了一个自定义的 MembershipProvider,除了一件小事,它工作得非常好,我想稍微扩展一下 MembershipUser 类。 默认是这样的: MembershipUser membe
是否可以控制通过调用 MembershipUser.ResetPassword() 自动生成的密码的格式? 我希望能够允许或不允许在生成的密码中使用某些特殊字符。 我使用密码格式为 Hashed 的
是否可以在不使用 MembershipUser 类的情况下创建自定义 MembershipProvider? 我想使用我自己的 User 类并保持整洁,我不想使用 MembershipUser 类,它
我遇到了 mvc4 成员资格问题,我无法像更改用户密码一样 public bool SetNewPassword(string newPassword) { var gener
我尝试在我自己的数据库中使用自定义 MemberShipUser 实现自定义 MembershipPriver(具有特定的用户表模型): 这是完全不同的文件: iTwitterMembershipPr
我无法设置 MembershipUser 对象的 Comments 属性。 例如: MembershipUser mu = Membership.GetUser(username);
我正在通过编写示例应用程序来学习 ASP.Net MVC3 EF4。我使用的是标准成员(member)提供程序。我在成员(member)提供者中有 requiresUniqueEmail="true"
如何通过提供电子邮件地址来检索 ASP.NET MembershipUser,Membership.GetUser() 的重载都不会获取电子邮件地址。 最佳答案 string thisUser= Me
我假设 MembershipUser 对象和实现 IPrincipal 接口(interface)的对象在某种意义上是“连接的”,即当其中一个对象中的某些信息发生变化时,另一个对象也会相应地发生变化?
我创建了自己的数据库架构来存储用户信息。 CREATE TABLE [dbo].[MyCustomMembership_Users]( [UserId] [uniqueidentifier] NOT
我正在尝试让一些用户在事件目录 (AD) 中更新他们的电子邮件地址。我正在尝试使用 MembershipUser 类来实现它。但是出现“一般访问被拒绝错误”。这是我的代码: strin
我是一名优秀的程序员,十分优秀!