• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ASP.NETSecurityProvider实现(二)Entity、Services

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 接“ASP.NET Security Provider实现(一)

为了实现MembershipProvider、RoleProvider、ProfileProvider,需要定义存储相关的实体类和业务逻辑相关的存储类。由于使用了MVCQuick.Framework的存储和IoC容器,实现了业务逻辑与持久化分离。 

实体

继承MVCQuick.Framework.EntityBase

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;

namespace MVCQuick.Framework.Security
{
///<summary>
/// 用户
///</summary>
[Serializable]
public class User : EntityBase
{
///<summary>
/// 登录名
///</summary>
public virtual string Username { get; set; }

///<summary>
/// 电子邮件地址
///</summary>
public virtual string Email { get; set; }

///<summary>
/// 密码
///</summary>
public virtual string Password { get; set; }

///<summary>
/// 密码格式
///</summary>
public virtual System.Web.Security.MembershipPasswordFormat PasswordFormat { get; set; }

///<summary>
/// 辅助密码验证
///</summary>
public virtual string PasswordSalt { get; set; }

///<summary>
/// 密码提示问题
///</summary>
public virtual string PasswordQuestion { get; set; }

///<summary>
/// 密码提示答案
///</summary>
public virtual string PasswordAnswer { get; set; }

///<summary>
/// 是否是匿名用户
///</summary>
public virtual bool IsAnonymous { get; set; }

///<summary>
/// 是否可以进行身份验证
///</summary>
public virtual bool IsApproved { get; set; }

///<summary>
/// 是否因被锁定而无法进行验证
///</summary>
public virtual bool IsLockedOut { get; set; }

///<summary>
/// 创建日期和时间
///</summary>
public virtual DateTime CreateDate { get; set; }

///<summary>
/// 最后一次进行身份验证或访问应用程序的日期和时间
///</summary>
public virtual DateTime LastActivityDate { get; set; }

///<summary>
/// 最后一次进行身份验证的日期和时间
///</summary>
public virtual DateTime LastLoginDate { get; set; }

///<summary>
/// 最后一次更新密码的日期和时间
///</summary>
public virtual DateTime LastPasswordChangedDate { get; set; }

///<summary>
/// 最后一次锁定的日期和时间
///</summary>
public virtual DateTime LastLockoutDate { get; set; }

///<summary>
/// 密码重试次数
///</summary>
public virtual int FailedPasswordAttemptCount { get; set; }

///<summary>
/// 密码失败尝试窗口打开的日期和时间
///</summary>
public virtual DateTime FailedPasswordAttemptWindowStart { get; set; }

///<summary>
/// 密码提示答案重试次数
///</summary>
public virtual int FailedPasswordAnswerAttemptCount { get; set; }

///<summary>
/// 密码提示答案失败尝试窗口打开的日期和时间
///</summary>
public virtual DateTime FailedPasswordAnswerAttemptWindowStart { get; set; }

///<summary>
/// 其它自定义信息
///</summary>
public virtual string Comment { get; set; }

///<summary>
/// 应用程序
///</summary>
public virtual Application Application { get; set; }

///<summary>
///
///</summary>
public virtual IEnumerable<Role> Roles { get; set; }

///<summary>
///
///</summary>
public User()
{
Roles = new List<Role>();
}

}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MVCQuick.Framework.Security
{
///<summary>
/// 角色
///</summary>
[Serializable]
public class Role : EntityBase
{
///<summary>
/// 名称
///</summary>
public virtual string Name { get; set; }

///<summary>
/// 说明
///</summary>
public virtual string Description { get; set; }

///<summary>
/// 应用程序
///</summary>
public virtual Application Application { get; set; }

///<summary>
///
///</summary>
public virtual IEnumerable<User> Users { get; set; }

///<summary>
///
///</summary>
public Role()
{
Users = new List<User>();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MVCQuick.Framework.Security
{
///<summary>
/// 用户配置文件
///</summary>
public class Profile : EntityBase
{
///<summary>
/// 用户Id
///</summary>
public virtual int UserId { get; set; }

///<summary>
/// 属性名称
///</summary>
public virtual string PropertyNames { get; set; }

///<summary>
/// 字符串值
///</summary>
public virtual string PropertyValuesString { get; set; }

/// 二进制值
///</summary>
public virtual byte[] PropertyValuesBinary { get; set; }

///<summary>
/// 最后一次更新时间
///</summary>
public virtual DateTime LastUpdatedDate { get; set; }

}
}

 

服务

用户服务类

MVCQuick.Framework.Security.UserService.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MVCQuick.Framework.Repository;

namespace MVCQuick.Framework.Security
{
public class UserService
{
IRepository repository;

public UserService(IRepository repository)
{
this.repository = repository;
}

public void CreateUser(User user)
{
repository.Save<User>(user);
}

public void UpdateUser(User user)
{
repository.Update<User>(user);
}

public void DeleteUser(User user)
{
repository.Delete<User>(user);
}

public User GetUser(int userID)
{
return repository.Get<int, User>(userID);
}

public User GetUserByName(string applicationName, string username)
{
Application application =
new ApplicationService(repository).GetApplication(applicationName);

User user = repository.FindOne<User>("Application", application, "Username", username);

return user;
}


public User GetUserByName(string applicationName, string username, bool updateLastActivity, DateTime currentTimeUtc)
{
Application application =
new ApplicationService(repository).GetApplication(applicationName);

User user = repository.FindOne<User>("Application", application, "Username", username);

if (user != null)
{
if (updateLastActivity)
{
user.LastActivityDate = currentTimeUtc;

UpdateUser(user);
}

return user;
}

return null;

}

public IList<User> GetUsersByEmail(string applicationName, string email)
{

Application application =
new ApplicationService(repository).GetApplication(applicationName);

return repository.Find<User>("Application", application, "Email", email);

}

public int UpdatePassword(string applicationName, string username, string newPassword, string passwordSalt,
System.Web.Security.MembershipPasswordFormat passwordFormat, DateTime currentTimeUtc)
{
Application application =
new ApplicationService(repository).GetApplication(applicationName);

User user = repository.FindOne<User>("Application", application, "Username", username);

if (user != null)
{
user.Password = newPassword;
user.PasswordSalt = passwordSalt;
user.PasswordFormat = passwordFormat;
user.LastPasswordChangedDate = currentTimeUtc;

UpdateUser(user);

return 0;
}
else
{
return 1;
}

}

public int ResetPassword(string applicationName, string username, string newPassword,
int maxInvalidPasswordAttempts, int passwordAttemptWindow, string passwordSalt,
System.Web.Security.MembershipPasswordFormat passwordFormat, string passwordAnswer,
DateTime currentTimeUtc)
{
Application application =
new ApplicationService(repository).GetApplication(applicationName);

User user = repository.FindOne<User>("Application", application, "Username", username);

if (user != null)
{
if (!user.IsLockedOut)
{
if (passwordAnswer == null || passwordAnswer.ToLower().Equals(user.PasswordAnswer))
{
user.Password = newPassword;
user.PasswordFormat = passwordFormat;
user.PasswordSalt = passwordSalt;
user.LastPasswordChangedDate = currentTimeUtc;
user.FailedPasswordAnswerAttemptCount = 0;
user.FailedPasswordAnswerAttemptWindowStart = new DateTime(1754, 1, 1);

UpdateUser(user);

return 0;
}
else
{
if (currentTimeUtc > user.FailedPasswordAnswerAttemptWindowStart.AddMinutes(passwordAttemptWindow))
{
user.FailedPasswordAnswerAttemptCount = 1;
user.FailedPasswordAnswerAttemptWindowStart = currentTimeUtc;
}
else
{
user.FailedPasswordAnswerAttemptCount++;
user.FailedPasswordAnswerAttemptWindowStart = currentTimeUtc;
}

if (user.FailedPasswordAnswerAttemptCount > maxInvalidPasswordAttempts)
{
user.IsLockedOut = true;
user.LastLockoutDate = currentTimeUtc;
}

UpdateUser(user);

return 3;
}
}
else
{
return 99;
}
}
else
{
return 1;
}
}

public int ChangePasswordQuestionAndAnswer(string applicationName, string username, string newPasswordQuestion,
string newPasswordAnswer)
{
Application application =
new ApplicationService(repository).GetApplication(applicationName);

User user = repository.FindOne<User>("Application", application, "Username", username);

if (user != null)
{
user.PasswordQuestion = newPasswordQuestion;
user.PasswordAnswer = newPasswordAnswer;

UpdateUser(user);

return 0;
}
else
{
return 1;
}

}

public int DeleteUser(string applicationName, string username, bool deleteAllRelatedData)
{
Application application =
new ApplicationService(repository).GetApplication(applicationName);

User user = repository.FindOne<User>("Application", application, "Username", username);

if (user != null)
{
DeleteUser(user);

return 0;
}
else
{
return 1;
}
}


public IList<User> FindUsersByEmail(string applicationName, string emailToMatch,
int pageIndex, int pageSize, out int totalRecords)
{
Application application =
new ApplicationService(repository).GetApplication(applicationName);

IList<User> users = repository.Find<User>("Application", application, "Email", "%"+emailToMatch+"%");

totalRecords = users.Count;

var result = users.Skip(pageIndex * pageSize).Take(pageSize);

return (IList<User>)result;
}

public IList<User> FindUsersByName(string applicationName, string usernameToMatch,
int pageIndex, int pageSize, out int totalRecords)
{
Application application =
new ApplicationService(repository).GetApplication(applicationName);

IList<User> users = repository.Find<User>("Application", application, "Username", "%" + usernameToMatch + "%");

totalRecords = users.Count;

var result = users.Skip(pageIndex * pageSize).Take(pageSize);

return (IList<User>)result;
}


public IList<User> GetAllUsers(string applicationName,
int pageIndex, int pageSize, out int totalRecords)
{
Application application =
new ApplicationService(repository).GetApplication(applicationName);

IList<User> users = repository.Find<User>("Application", application);

totalRecords = users.Count;

var result = users.Skip(pageIndex * pageSize).Take(pageSize);

return (IList<User>)result;
}


public long GetNumberOfUsersOnline(string applicationName, int minutesSinceLastInActive, DateTime currentTimeUtc)
{
Application application =
new ApplicationService(repository).GetApplication(applicationName);

IList<User> users = repository.Find<User>("Application", application);

long count = 0;

DateTime activeDate = currentTimeUtc.AddMinutes(-minutesSinceLastInActive);

foreach (var user in users)
{
if (user.LastActivityDate > activeDate) count++;

}

return count;
}

public User GetUserByUserID(int userID, bool updateLastActivity, DateTime currentTimeUtc)
{
User user = repository.Get<int, User>(userID);

if (user != null)
{
if (updateLastActivity)
{
user.LastActivityDate = currentTimeUtc;

UpdateUser(user);
}

return user;
}
else
{
return null;
}
}



public int UnlockUser(string applicationName, string username)
{
Application application =
new ApplicationService(repository).GetApplication(applicationName);

User user = repository.FindOne<User>("Application", application, "Username", username);

if (user != null)
{
user.LastLockoutDate = new DateTime(1754, 1, 1);
user.FailedPasswordAttemptCount = 0;
user.FailedPasswordAttemptWindowStart = new DateTime(1754, 1, 1);
user.FailedPasswordAnswerAttemptCount = 0;
user.FailedPasswordAnswerAttemptWindowStart = new DateTime(1754, 1, 1);

UpdateUser(user);

return 0;
}
else
{
return 1;
}
}

public int UpdateUser(string applicationName, string username, string email, string comment, bool isApproved,
DateTime lastLoginDate, DateTime lastActivityDate, bool uniqueEmail)
{
Application application =
new ApplicationService(repository).GetApplication(applicationName);

User user = repository.FindOne<User>("Application", application, "Username", username);

if (user != null)
{
if (uniqueEmail)
{
IList<User> users2 = repository.Find<User>("Application", application, "Email", email);

if (users2.Count > 1)
{
return 7;
}

if (users2.Count == 1 && ((User)users2[0]).Id != user.Id)
{
return 7;
}
}

user.Email = email;
user.IsApproved = isApproved;
user.LastActivityDate = lastActivityDate;
user.LastLoginDate = lastLoginDate;
user.Comment = comment;

UpdateUser(user);
return 0;
}
else
{
return 1;
}
}

public int UpdateUserInfo(string applicationName, string username, bool isPasswordCorrect, int passwordAttemptWindow,
int maxInvalidPasswordAttempts, bool updateLastLoginActivityDate, DateTime currentTimeUtc)
{
Application application =
new ApplicationService(repository).GetApplication(applicationName);

User user = repository.FindOne<User>("Application", application, "Username", username);

if (user != null)
{
if (!user.IsLockedOut)
{
if (!isPasswordCorrect)
{
if (currentTimeUtc > user.FailedPasswordAttemptWindowStart.AddMinutes(passwordAttemptWindow))
{
user.FailedPasswordAttemptCount = 1;
user.FailedPasswordAttemptWindowStart = currentTimeUtc;
}
else
{
user.FailedPasswordAttemptCount++;
user.FailedPasswordAttemptWindowStart = currentTimeUtc;
}

if (user.FailedPasswordAttemptCount > maxInvalidPasswordAttempts)
{
user.IsLockedOut = true;
user.LastLockoutDate = currentTimeUtc;
}

UpdateUser(user);

return 2;
}
else
{
user.LastLockoutDate = new DateTime(1754, 1, 1);
user.FailedPasswordAttemptCount = 0;
user.FailedPasswordAttemptWindowStart = new DateTime(1754, 1, 1);
user.FailedPasswordAnswerAttemptCount = 0;
user.FailedPasswordAnswerAttemptWindowStart = new DateTime(1754, 1, 1);

if (updateLastLoginActivityDate)
{
user.LastActivityDate = DateTime.UtcNow;
user.LastLoginDate = DateTime.UtcNow;
}

UpdateUser(user);

return 0;
}
}
else
{
return 99;
}
}
else
{
return 1;
}
}


public string GetPassword(string applicationName, string username, string passwordAnswer,
bool requiresQuestionAndAnswer, int maxInvalidPasswordAttempts, int passwordAttemptWindow,
out System.Web.Security.MembershipPasswordFormat passwordFormat, out int status,
DateTime currentTimeUtc)
{

Application application =
new ApplicationService(repository).GetApplication(applicationName);

User user = repository.FindOne<User>("Application", application, "Username", username);

if (user != null)
{
if (!user.IsLockedOut)
{
if (requiresQuestionAndAnswer)
{
if (passwordAnswer == null || (!passwordAnswer.ToLower().Equals(user.PasswordAnswer)))
{
if (currentTimeUtc > user.FailedPasswordAnswerAttemptWindowStart.AddMinutes(passwordAttemptWindow))
{
user.FailedPasswordAnswerAttemptCount = 1;
user.FailedPasswordAnswerAttemptWindowStart = currentTimeUtc;
}
else
{
user.FailedPasswordAnswerAttemptCount++;
user.FailedPasswordAnswerAttemptWindowStart = currentTimeUtc;
}

if (user.FailedPasswordAnswerAttemptCount > maxInvalidPasswordAttempts)
{
user.IsLockedOut = true;
user.LastLockoutDate = currentTimeUtc;
}

status = 3;

UpdateUser(user);
}
else
{
user.FailedPasswordAnswerAttemptCount = 0;
user.FailedPasswordAnswerAttemptWindowStart = new DateTime(1754, 1, 1);

UpdateUser(user);

status = 0;
passwordFormat = user.PasswordFormat;
return user.Password;
}
}
else
{
status = 0;
passwordFormat = user.PasswordFormat;
return user.Password;
}
}
else
{
status = 99;
}
}
else
{
status = 1;
}

passwordFormat = System.Web.Security.MembershipPasswordFormat.Clear;

return null;
}
}
}

角色服务类

MVCQuick.Framework.Security.RoleService.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MVCQuick.Framework.

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
ASP.NET设置网站图标发布时间:2022-07-10
下一篇:
Asp.net页面传值总结(转载)发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap