当前位置:编程学习 > C#/ASP.NET >>

一个基于asp和C#的网站,已经上线,为什么我人工向数据库中user的表格中增加用户,网站就不认呢?

数据库是MS sql server 2008R2, 现在客户需要增加两个管理员账户,我也在数据库的user的表格中根据以前管理员账户的格式信息手工添加了两个账户信息。但从网站登录后,登录页面转向一个非常简单的页面(这个页面是guest用户的登录页面),而不是转向管理员的页面?总之,网站似乎不认识这个账户,为什么呢?

是不是修改数据库还要重新编译呢?还是什么其他原因呢? --------------------编程问答-------------------- 要么你程序写的有漏洞,要么看看是否有缓存机制,将之前的数据做了缓存,并没有获取最新数据 --------------------编程问答-------------------- 首先申明我是菜鸟啊,最近都在帮别人擦屁股,我对C#不熟悉。(说明一下,我对基本知识不了解,请别见怪)

应该不是缓存问题,因为我昨天到今天测了很多次,而且还有其他人从其他地方登陆也进行测试,还是这样。

--------------------编程问答-------------------- 在我自己的电脑上运行时,出现了两个错误:
1. 
Error 2 Unexpected error creating debug information file 'C:\Users\Erik Zhou\Documents\Visual Studio 2010\Projects\MOR1\License\obj\Debug\License.PDB' -- 'C:\Users\Erik Zhou\Documents\Visual Studio 2010\Projects\MOR1\License\obj\Debug\License.pdb: Access is denied.
'

2. 
Error 4 Could not write lines to file "obj\Debug\License.csproj.FileListAbsolute.txt". Access to the path 'C:\Users\Erik Zhou\Documents\Visual Studio 2010\Projects\MOR1\License\obj\Debug\License.csproj.FileListAbsolute.txt' is denied. C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets 3658

这两个错误啥意思啊?

引用 1 楼 bdmh 的回复:
要么你程序写的有漏洞,要么看看是否有缓存机制,将之前的数据做了缓存,并没有获取最新数据
--------------------编程问答-------------------- 晕死,是只读的设定,在本机编译时的错误信息排除了.

那还是那个问题,为什么直接修改数据库不行呢?有什么其他原因呢? --------------------编程问答-------------------- 很有可能你的程序不是简单的就读了user那一张表,是否还有权限(绑定菜单功能之类的)。然后程序中加载时(sql关联查询),并没有查到这个用户(sql关联查询时过滤了你添加的这个账号)。 --------------------编程问答-------------------- 很靠谱的想法。这个网站的作者很喜欢关联查询。

因为我对C#和ms sql server的知识基本为零,能否请你指点这个问题:
我用的是Visual Studio 2010,此刻在我面前的vs 2010界面有四部分,如下图。

尤其特别注意,左边是所有文件以及代码的树形结构的窗口。原作者喜欢把关于database的部分放在一起,请大神们看看,我应该查询那个部分?以及大概什么文件呢?小弟我搞php+mysql的,找了半天,也没有找到貌似链接sql server语句?

请大神们谅解,我这段时间专门来擦别人的屁股,自己也不懂C#,只能一点点问清楚了,还请大神们讲讲有用的细节,我能够顺藤摸瓜的。
谢谢。


引用 5 楼 guwei4037 的回复:
很有可能你的程序不是简单的就读了user那一张表,是否还有权限(绑定菜单功能之类的)。然后程序中加载时(sql关联查询),并没有查到这个用户(sql关联查询时过滤了你添加的这个账号)。
--------------------编程问答-------------------- 从你截的图上的代码来看,用的是Entity Framework(EF)操作数据库的,是微软C#的ORM(类似于java中的hibernate)。你可以把光标放到MOREntities上面,按F12定位到MOREntities的定义部分。
http://blog.csdn.net/chinacsharper/article/details/9368855 --------------------编程问答-------------------- 对了,我发现了一个UserManager.cs的文件,里面很像搜索user并判断的,上代码,还请大神们指点:
[code=csharp]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using System.Data.Objects.SqlClient;
using DataLayer;


namespace Users
{
    public class UserManager
    {
        public static bool Authenticate(string userID, string password, out bool isAdmin)
        {

            MOREntities enity = new MOREntities();

            var v = from s in enity.Users where s.UserID == userID && s.Password == password select s;


            if (v.Count() > 0)
            {
                

                isAdmin = v.First<DataLayer.User>().IsAdmin;

                return true;

            }
            else
            {
                isAdmin = false;
                return false;
            }

        }

        private static string[] SetDefaultValues(int year, int month, string[] arr)
        {

            int days = DateTime.DaysInMonth(year,month);

            for (int i = 1; i <= days; i++)
            {
                arr[i] = "-";
            }

            return arr;

        }

        public static string GetHLinfo(string userId)
        {
            DataLayer.User user = GetUserObject(userId);

            if (user != null)
            {
                if (user.HLinfo != null)
                { return user.HLinfo; }
                else
                { return "No information available at present."; }
            }
            else
            {
                return string.Empty;
            }

        }
        public static void EditHLinfo(string userID, string HLinfo)
        {
            MOREntities entity = new MOREntities();

            var user = from a in entity.Users where a.UserID == userID select a;

            if (user.Count() > 0)
            {
                user.First().HLinfo = HLinfo;
                entity.SaveChanges();

            }

        }
        

        public static List<string[]> GetLoginHistory(int month, int year)
        {
            MOREntities entity = new MOREntities();

            const int numCols = 32;

            string user = "";

            string[] value = null;
    
            List<string[]> values = new List<string[]>();

            dynamic history = (from a in entity.LoginHistories group a by new { UserId = a.UserId, LoginTime = EntityFunctions.TruncateTime(a.LoginTime) } into grp where grp.Key.LoginTime.Value.Month == month && grp.Key.LoginTime.Value.Year == year
                               orderby grp.Key.UserId select new { UserId = grp.Key.UserId, Date = EntityFunctions.TruncateTime(grp.Key.LoginTime), LoginCount = grp.Count() }).ToArray();

            int i = 0;

            while (i < history.Length)
            {
                if (i == 0)
                {
                    value = new string[numCols];

                    value = SetDefaultValues(year, month, value);


                    value[0] = history[i].UserId;
                    value[history[i].Date.Day] = history[i].LoginCount.ToString();
                }
                else
                {
                    if (history[i].UserId == history[i - 1].UserId)
                    {
                        value[history[i].Date.Day] = history[i].LoginCount.ToString();
                    }
                    else
                    {
                        values.Add(value);
                        value = new string[numCols];

                        SetDefaultValues(year, month, value);

                        value[0] = history[i].UserId;
                        user = history[i].UserId;
                        value[history[i].Date.Day] = history[i].LoginCount.ToString();
                    }
                }

                i++;
            }

            values.Add(value);

            return values;

        }

        public static void ReportLogin(string userId, DateTime time)
        {
            MOREntities entity = new MOREntities();

            LoginHistory login = new LoginHistory();

            login.UserId = userId;
            login.LoginTime = time;

            entity.LoginHistories.AddObject(login);

            entity.SaveChanges();

        }

        public static void ChangePassword(string userID, string password)
        {
            MOREntities entity = new MOREntities();

            var user = from a in entity.Users where a.UserID == userID select a;

            if (user.Count() > 0)
            {
                user.First().Password = password;
                entity.SaveChanges();

            }

        }

        public static string GetPassword(string userId)
        {
            DataLayer.User user = GetUserObject(userId);

            if (user != null)
            {
                return user.Password;
            }

            return "";

        }

        public static void EditUser(string userId, int weight, int waist, int length, int wantedWeight)
        {
            MOREntities entity = new MOREntities();

            var user = from v in entity.UserAttributes where v.UserId == userId select v;

            if (user.Count() > 0)
            {
                user.First().Waist = waist;
                user.First().StartWeight = weight;
                user.First().WantedWeight = wantedWeight;
                user.First().Length = length;
                
                entity.SaveChanges();
            }
            else
            {
                UserAttribute attribute = new UserAttribute();

                attribute.Length = length;
                attribute.UserId = userId;
                attribute.Waist = waist;
                attribute.WantedWeight = wantedWeight;
                attribute.StartWeight = weight;
                entity.UserAttributes.AddObject(attribute);
                entity.SaveChanges();
            }

        } --------------------编程问答-------------------- 第二部分代码是:


       public static int CalcWeightChange(string userId)
        {
            MOREntities entity = new MOREntities();

            double weight1 = 0.0;
            double weight2 = 0.0;

            double weightChange = 0.0;

            var j = from i in entity.WeightReports where i.UserId == userId orderby i.ReportedDate select i;

            List<WeightReport> reports = j.ToList();

            if (reports.Count() > 1)
            {
                weight2 = reports[reports.Count() - 2].Weight;
                weight1 = reports[reports.Count() - 1].Weight;

                weightChange = weight1 - weight2;
            }

            return (int) Math.Round(weightChange);

        }

        public List<string[]> GetUsers()
        {
            MOREntities entity = new MOREntities();

            var s = from v in entity.Users where v.IsAdmin == false orderby v.UserID select v;

            string[] userData = new string[2];

            List<string[]> userList = new List<string[]>();

            foreach (DataLayer.User user in s)
            {
                userData = new string[2];

                userData[0] = user.UserID;

                userData[1] = CalcWeightChange(user.UserID).ToString();
                userList.Add(userData);
            }

            return userList;

        }

        public static void DeleteUser(string userID)
        {
            MOREntities entity = new MOREntities();

            DataLayer.User user = (from s in entity.Users where s.UserID == userID select s).First<DataLayer.User>();

            entity.Users.DeleteObject(user);

            var report = (from v in entity.WeightReports where v.UserId == userID select v);

            if (report.Count() > 0)
            {
                entity.WeightReports.DeleteObject(report.First<WeightReport>());
            }

            var report1 = (from j in entity.WaistReports where j.UserId == userID select j);

            if (report1.Count() > 0)
            {
                entity.WaistReports.DeleteObject(report1.First());
            }

            var attribute = (from a in entity.UserAttributes where a.UserId == userID select a);

            if (attribute.Count() > 0)
            {
                entity.UserAttributes.DeleteObject(attribute.First());
            }

            entity.SaveChanges();

        }

        public static DateTime? GetRegistrationDate(string userID)
        {
            MOREntities enity = new MOREntities();

            var name = from s in enity.Users where s.UserID == userID select s;

            if (name.Count() > 0)
            {
                return name.ToList<DataLayer.User>()[0].RegistrationDate;
            }
            else
            {
                return null;
            }

        }



        public static DataLayer.User GetUserObject(string userID)
        {

            MOREntities entity = new MOREntities();

            var name = from s in entity.Users where s.UserID == userID select s;

            if (name.Count() > 0)
            {
                return name.ToList<DataLayer.User>()[0];
            }
            else
            {
                return null;
            }

        }

        public static double GetWaist(string userId)
        {
            MOREntities entity = new MOREntities();

            var s = from v in entity.UserAttributes where v.UserId == userId select v;

            if (s.Count() > 0)
            {
                return s.First<UserAttribute>().Waist;

            }
            else
            {
                return 0.0;
            }

        }

        public static double GetLength(string userId)
        {
            MOREntities entity = new MOREntities();

            var s = from v in entity.UserAttributes where v.UserId == userId select v;

            if (s.Count() > 0)
            {
                return s.First<UserAttribute>().Length;

            }
            else
            {
                return 0.0;
            }

        }

        public static double GetStartWeight(string userId)
        {
            MOREntities entity = new MOREntities();

            var s = from v in entity.UserAttributes where v.UserId == userId select v;

            if (s.Count() > 0)
            {
                return s.First<UserAttribute>().StartWeight;

            }
            else
            {
                return 0.0;
            }

        }

        public static double GetWantedWeight(string userId)
        {
            MOREntities entity = new MOREntities();

            var s = from v in entity.UserAttributes where v.UserId == userId select v;

            if (s.Count() > 0)
            {
                return s.First<UserAttribute>().WantedWeight;

            }
            else
            {
                return 0.0;
            }

        }

        public static bool UserExists(string userId)
        {
            MOREntities entity = new MOREntities();

            var exist = from a in entity.Users where a.UserID == userId select a;

            return (exist.Count() > 0);

        }


--------------------编程问答--------------------
引用 7 楼 guwei4037 的回复:
从你截的图上的代码来看,用的是Entity Framework(EF)操作数据库的,是微软C#的ORM(类似于java中的hibernate)。你可以把光标放到MOREntities上面,按F12定位到MOREntities的定义部分。
http://blog.csdn.net/chinacsharper/article/details/9368855


准确定位,我再琢磨琢磨,有问题还要过来向各位大神请教啊。

后面肯定结贴。 --------------------编程问答-------------------- 可以单步调试下了。。。 --------------------编程问答-------------------- 同意楼上的观点,你在Login的方法主体内调试下,或者查询下所有的User,也可以Console到输出窗口,首先看看数据是否存在,其次看看你添加的数据和其他正常的数据有什么不一样的地方 --------------------编程问答-------------------- 看一下user表里的是否有IsAdmin及对应的值。 --------------------编程问答-------------------- 感谢楼上大神们的建议,目前情况是这样的:
我用一个管理员帐号和我加入数据库的账号(我设定了数据库表格里一切看起来是管理员的值,表中字段IsAdmin设定为True)登录网站,一步一步查询,发现这个语句给了不同的值:
var v = from s in enity.Users where s.UserID == userID && s.Password == password select s;


            if (v.Count() > 0)
            {
                

                isAdmin = v.First<DataLayer.User>().IsAdmin;

                return true;

            }


其中isAdmin = v.First<DataLayer.User>().IsAdmin;给了两个账户一个是True,另外一个是False,
我按F12追踪User, 看到User的类定义:
 public partial class User : EntityObject
    {
        #region Factory Method
    
        /// <summary>
        /// Create a new User object.
        /// </summary>
        /// <param name="password">Initial value of the Password property.</param>
        /// <param name="isAdmin">Initial value of the IsAdmin property.</param>
        /// <param name="registrationDate">Initial value of the RegistrationDate property.</param>
        /// <param name="userID">Initial value of the UserID property.</param>
        public static User CreateUser(global::System.String password, global::System.Boolean isAdmin, global::System.DateTime registrationDate, global::System.String userID)
        {
            User user = new User();
            user.Password = password;
            user.IsAdmin = isAdmin;
            user.RegistrationDate = registrationDate;
            user.UserID = userID;
            return user;
        }

        #endregion
        #region Primitive Properties
    
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
        [DataMemberAttribute()]
        public global::System.String Email
        {
            get
            {
                return _Email;
            }
            set
            {
                OnEmailChanging(value);
                ReportPropertyChanging("Email");
                _Email = StructuralObject.SetValidValue(value, true);
                ReportPropertyChanged("Email");
                OnEmailChanged();
            }
        }
        private global::System.String _Email;
        partial void OnEmailChanging(global::System.String value);
        partial void OnEmailChanged();
    
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
        [DataMemberAttribute()]
        public global::System.String Password
        {
            get
            {
                return _Password;
            }
            set
            {
                OnPasswordChanging(value);
                ReportPropertyChanging("Password");
                _Password = StructuralObject.SetValidValue(value, false);
                ReportPropertyChanged("Password");
                OnPasswordChanged();
            }
        }
        private global::System.String _Password;
        partial void OnPasswordChanging(global::System.String value);
        partial void OnPasswordChanged();
    
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
        [DataMemberAttribute()]
        public global::System.Boolean IsAdmin
        {
            get
            {
                return _IsAdmin;
            }
            set
            {
                OnIsAdminChanging(value);
                ReportPropertyChanging("IsAdmin");
                _IsAdmin = StructuralObject.SetValidValue(value);
                ReportPropertyChanged("IsAdmin");
                OnIsAdminChanged();
            }
        }
        private global::System.Boolean _IsAdmin;
        partial void OnIsAdminChanging(global::System.Boolean value);
        partial void OnIsAdminChanged();
    
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
        [DataMemberAttribute()]
        public global::System.DateTime RegistrationDate
        {
            get
            {
                return _RegistrationDate;
            }
            set
            {
                OnRegistrationDateChanging(value);
                ReportPropertyChanging("RegistrationDate");
                _RegistrationDate = StructuralObject.SetValidValue(value);
                ReportPropertyChanged("RegistrationDate");
                OnRegistrationDateChanged();
            }
        }
        private global::System.DateTime _RegistrationDate;
        partial void OnRegistrationDateChanging(global::System.DateTime value);
        partial void OnRegistrationDateChanged();
    
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
        [DataMemberAttribute()]
        public global::System.String Firstname
        {
            get
            {
                return _Firstname;
            }
            set
            {
                OnFirstnameChanging(value);
                ReportPropertyChanging("Firstname");
                _Firstname = StructuralObject.SetValidValue(value, true);
                ReportPropertyChanged("Firstname");
                OnFirstnameChanged();
            }
        }
        private global::System.String _Firstname;
        partial void OnFirstnameChanging(global::System.String value);
        partial void OnFirstnameChanged();
    
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
        [DataMemberAttribute()]
        public global::System.String Lastname
        {
            get
            {
                return _Lastname;
            }
            set
            {
                OnLastnameChanging(value);
                ReportPropertyChanging("Lastname");
                _Lastname = StructuralObject.SetValidValue(value, true);
                ReportPropertyChanged("Lastname");
                OnLastnameChanged();
            }
        }
        private global::System.String _Lastname;
        partial void OnLastnameChanging(global::System.String value);
        partial void OnLastnameChanged();
    
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
        [DataMemberAttribute()]
        public global::System.String UserID
        {
            get
            {
                return _UserID;
            }
            set
            {
                if (_UserID != value)
                {
                    OnUserIDChanging(value);
                    ReportPropertyChanging("UserID");
                    _UserID = StructuralObject.SetValidValue(value, false);
                    ReportPropertyChanged("UserID");
                    OnUserIDChanged();
                }
            }
        }
        private global::System.String _UserID;
        partial void OnUserIDChanging(global::System.String value);
        partial void OnUserIDChanged();
    
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
        [DataMemberAttribute()]
        public global::System.String HLinfo
        {
            get
            {
                return _HLinfo;
            }
            set
            {
                OnHLinfoChanging(value);
                ReportPropertyChanging("HLinfo");
                _HLinfo = StructuralObject.SetValidValue(value, true);
                ReportPropertyChanged("HLinfo");
                OnHLinfoChanged();
            }
        }
        private global::System.String _HLinfo;
        partial void OnHLinfoChanging(global::System.String value);
        partial void OnHLinfoChanged();
    
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
        [DataMemberAttribute()]
        public Nullable<global::System.Boolean> NoDebt
        {
            get
            {
                return _NoDebt;
            }
            set
            {
                OnNoDebtChanging(value);
                ReportPropertyChanging("NoDebt");
                _NoDebt = StructuralObject.SetValidValue(value);
                ReportPropertyChanged("NoDebt");
                OnNoDebtChanged();
            }
        }
        private Nullable<global::System.Boolean> _NoDebt = false;
        partial void OnNoDebtChanging(Nullable<global::System.Boolean> value);
        partial void OnNoDebtChanged();

        #endregion
    
    }


就是这个类的引用让这两个账户让变量isAdmin有了不同的值。

大神们,你们看这个类哪里出了问题呢?

对了,我在web.config中看到对用户进入网页的权限进行了规定,我把所有原来正确的管理员账号的allow的部分都拷贝给了第二个账户,但是仍然不管用。一个朋友说要进入网页上的asp.net administrative tool 进行设定。对么?



引用 13 楼 danding_ge 的回复:
看一下user表里的是否有IsAdmin及对应的值。


引用 12 楼 wpfLove 的回复:
同意楼上的观点,你在Login的方法主体内调试下,或者查询下所有的User,也可以Console到输出窗口,首先看看数据是否存在,其次看看你添加的数据和其他正常的数据有什么不一样的地方


引用 11 楼 ejason 的回复:
可以单步调试下了。。。


引用 5 楼 guwei4037 的回复:
很有可能你的程序不是简单的就读了user那一张表,是否还有权限(绑定菜单功能之类的)。然后程序中加载时(sql关联查询),并没有查到这个用户(sql关联查询时过滤了你添加的这个账号)。
--------------------编程问答-------------------- 晕死,我在哪里乱设一通,现在什么账户都进不去系统了。出错信息是:
Server Error in '/' Application.

Access is denied.

Description: An error occurred while accessing the resources required to serve this request. The server may not be configured for access to the requested URL. 

Error message 401.2.: Unauthorized: Logon failed due to server configuration.  Verify that you have permission to view this directory or page based on the credentials you supplied and the authentication methods enabled on the Web server.  Contact the Web server's administrator for additional assistance.

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1016 --------------------编程问答-------------------- 一个帐号和密码,你竟然得到了两个人的信息,说明你的帐号和密码信息重复了,如果你使用First得到的第一条数据估计就是那个IsAdmin为false的那条数据 --------------------编程问答--------------------
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
        [DataMemberAttribute()]
        public global::System.Boolean IsAdmin
        {
            get
            {
                return _IsAdmin;
            }
            set
            {
                OnIsAdminChanging(value);
                ReportPropertyChanging("IsAdmin");
                _IsAdmin = StructuralObject.SetValidValue(value);
                ReportPropertyChanged("IsAdmin");
                OnIsAdminChanged();
            }
        }
        private global::System.Boolean _IsAdmin;
        partial void OnIsAdminChanging(global::System.Boolean value);
        partial void OnIsAdminChanged();

你应该也注意到这段了吧!关键方法就是StructuralObject.SetValidValue,研究一下吧! --------------------编程问答-------------------- 感谢大家的帮助,虽然事情没有解决,但我准备把问题总结一下,再开一贴。
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,