为了账号安全,请及时绑定邮箱和手机立即绑定

如何为ASP.NET MVC 2创建自定义成员资格提供程序?

如何为ASP.NET MVC 2创建自定义成员资格提供程序?

C#
莫回无 2019-08-12 09:49:40
如何为ASP.NET MVC 2创建自定义成员资格提供程序?如何基于ASP.NET成员资格提供程序为ASP.NET MVC 2创建自定义成员资格?
查看完整描述

3 回答

?
慕森卡

TA贡献1806条经验 获得超8个赞

它也可以用更少量的代码来使用它,我不完全确定这种方法是否安全,但对你使用的任何数据库都能很好地工作。

在global.asax中

protected void Application_AuthenticateRequest(object sender, EventArgs e)
    {
        if (HttpContext.Current.User != null)
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                if (HttpContext.Current.User.Identity is FormsIdentity)
                {
                    FormsIdentity id =
                        (FormsIdentity)HttpContext.Current.User.Identity;
                    FormsAuthenticationTicket ticket = id.Ticket;

                    // Get the stored user-data, in this case, our roles
                    string userData = ticket.UserData;
                    string[] roles = userData.Split(',');
                    HttpContext.Current.User = new GenericPrincipal(id, roles);
                }
            }
        }
    }

它的作用是它从authCookie中读取由FormsAuthenticationTicket创建的角色

并且登录逻辑看起来像这样

public class dbService{
    private databaseDataContext db = new databaseDataContext();

    public IQueryable<vwPostsInfo> AllPostsAndDetails()
    {
        return db.vwPostsInfos;
    }

    public IQueryable<role> GetUserRoles(int userID)
    {
        return (from r in db.roles
                    join ur in db.UsersRoles on r.rolesID equals ur.rolesID                    where ur.userID == userID                    select r);
    }

    public IEnumerable<user> GetUserId(string userName)
    {
        return db.users.Where(u => u.username.ToLower() == userName.ToLower());
    }

    public bool logOn(string username, string password)
    {
        try
        {
            var userID = GetUserId(username);
            var rolesIQueryable = GetUserRoles(Convert.ToInt32(userID.Select(x => x.userID).Single()));
            string roles = "";
            foreach (var role in rolesIQueryable)
            {
                roles += role.rolesName + ",";
            }

            roles.Substring(0, roles.Length - 2);
            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                       1, // Ticket version
                       username, // Username associated with ticket
                       DateTime.Now, // Date/time issued
                       DateTime.Now.AddMinutes(30), // Date/time to expire
                       true, // "true" for a persistent user cookie
                       roles, // User-data, in this case the roles
                       FormsAuthentication.FormsCookiePath);// Path cookie valid for

            // Encrypt the cookie using the machine key for secure transport
            string hash = FormsAuthentication.Encrypt(ticket);
            HttpCookie cookie = new HttpCookie(
               FormsAuthentication.FormsCookieName, // Name of auth cookie
               hash); // Hashed ticket

            // Set the cookie's expiration time to the tickets expiration time
            if (ticket.IsPersistent) cookie.Expires = ticket.Expiration;

            // Add the cookie to the list for outgoing response
            HttpContext.Current.Response.Cookies.Add(cookie);

            return true;
        }
        catch
        {
            return (false);
        }
    }}

我使用两个表将角色存储在我的数据库中:table:具有列的角色:roleID和roleName以及表:UsersRoles具有列:userID和roleID,这使得多个用户可以使用多个角色,并且很容易制定自己的逻辑来添加/删除用户的角色等等。这使您可以使用[Authorize(Roles =“Super Admin”)]。希望这可以帮助。

编辑:忘记进行密码检查但是你只需在logOn方法中添加一个if,它检查提供的用户名和密码是否检查,如果没有则返回false


查看完整回答
反对 回复 2019-08-12
  • 3 回答
  • 0 关注
  • 327 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信