如何在Web应用程序中使用配置文件

时间:2021-07-24 06:33:17

I moved to web application from web site to improve it, I have profile in my web site.

我从网站转移到Web应用程序来改进它,我在我的网站上有个人资料。

My profile didn't work, so I implement System.Web.ProfileProvider and create a class ProfileCommon inherit ProfileBase I use HttpContext.Current.Profile.SetPropertyValue and GetPropertyValue. it works but the problem is it has a logically bug because HttpContext.Current.Profile doesn't call my ProfileProvider method How I can repair this ? or maybe is there any other implementation for profile in web application ?

我的配置文件不起作用,所以我实现System.Web.ProfileProvider并创建一个类ProfileCommon继承ProfileBase我使用HttpContext.Current.Profile.SetPropertyValue和GetPropertyValue。它的工作原理,但问题是它有一个逻辑错误,因为HttpContext.Current.Profile不会调用我的ProfileProvider方法我怎么能修复它?或者在Web应用程序中是否有任何其他的配置文件实现?

    public class ProfileCommon : ProfileBase
{
    public string FirstName 
    {
        get
        {
            return HttpContext.Current.Profile.GetPropertyValue("FirstName").ToString();
        }
        set
        {
            HttpContext.Current.Profile.SetPropertyValue("FirstName", value);
        }
    }
}

in the web.config

在web.config中

<profile inherits="BaniBookWebApp.Code.Core.ProfileCommon">
  <providers>
    <add name="CustomProfileProvider" type="BaniBookWebApp.Code.Core.CustomProfileProvider"/> 
  </providers>     
</profile>

CustomProfileProvider :

CustomProfileProvider:

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

namespace AccessControl
{
    public class ProfileProvider : System.Web.Profile.ProfileProvider
    {
        public ProfileProvider()
        {
        }

        public override int DeleteInactiveProfiles(System.Web.Profile.ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate)
        {
            int res = -1;
            using (var db = new eLearn2Entities())
            {
                List<Profile> profiles = (from m in db.Profiles
                                                 where (m.LastUpdatedDate.CompareTo(userInactiveSinceDate) < 0)
                                                 select m).ToList();
                foreach (Profile profile in profiles)
                {
                    if (profile != null)
                    {
                        db.Profiles.DeleteObject(profile);
                        res = db.SaveChanges();
                    }
                }
            }
            return res;
        }

        public override int DeleteProfiles(string[] usernames)
        {
            int res = -1;
            using (var db = new eLearn2Entities())
            {
                foreach (string username in usernames)
                {
                    Profile profile = (from m in db.Profiles
                                              join n in db.Users on m.UserId equals n.UserId
                                              where n.UserName == username
                                              select m).SingleOrDefault();
                    if (profile != null)
                    {
                        db.Profiles.DeleteObject(profile);
                        res = db.SaveChanges();
                    }
                }
            }
            return res;
        }

        public override int DeleteProfiles(System.Web.Profile.ProfileInfoCollection profiles)
        {
            throw new NotImplementedException();
        }      

        public override System.Configuration.SettingsPropertyValueCollection GetPropertyValues(System.Configuration.SettingsContext context, System.Configuration.SettingsPropertyCollection collection)
        {
            System.Configuration.SettingsPropertyValueCollection spvc = new System.Configuration.SettingsPropertyValueCollection();
            lock (collection.SyncRoot)
            {
                foreach (object item in collection)
                {
                    SettingsProperty sp = (SettingsProperty)item;
                    SettingsPropertyValue spv = new SettingsPropertyValue(sp);
                    spvc.Add(spv);
                }
            }
            return spvc;
        }

        public override void SetPropertyValues(System.Configuration.SettingsContext context, System.Configuration.SettingsPropertyValueCollection collection)
        {
            string PropertyNames_ = string.Empty;
            string PropertyValuesString_ = string.Empty;
            string userID = string.Empty;
            lock (collection.SyncRoot)
            {
                foreach (object item in collection)
                {
                    SettingsPropertyValue spv = (SettingsPropertyValue)item;
                    if (spv.PropertyValue != null)
                    {
                        if (!string.IsNullOrEmpty(spv.PropertyValue.ToString()))
                        {
                            if (spv.Name.Equals("UserID"))
                            {
                                userID = spv.PropertyValue.ToString();
                            }
                            else
                            {
                                PropertyNames_ += spv.Name + ";";
                                PropertyValuesString_ += spv.PropertyValue.ToString() + ";";
                            }
                        }
                    }
                }//foreach            
            }//lock

            try
            {
                using (var db = new eLearn2Entities())
                {
                    bool isAuthenticated = bool.Parse(context["IsAuthenticated"].ToString());

                    Profile profile = new Profile()
                    {
                        UserId = Guid.Parse(userID),                       
                        PropertyValuesString = PropertyValuesString_,
                        PropertyNames = PropertyNames_,
                        LastUpdatedDate = DateTime.UtcNow
                    };
                    db.Profiles.AddObject(profile);
                    db.SaveChanges();
                }
            }
            catch
            {
                using (var db = new eLearn2Entities())
                {
                    //bookmark gives me an error said multiple record
                    Guid myID = Guid.Parse(userID);
                    Profile existed_profile = db.Profiles.Where
                        (item => item.UserId == myID).SingleOrDefault() as Profile;

                    existed_profile.PropertyValuesString = PropertyValuesString_;
                    existed_profile.PropertyNames = PropertyNames_;
                    existed_profile.LastUpdatedDate = DateTime.UtcNow;

                    db.Profiles.ApplyOriginalValues(existed_profile);
                    db.SaveChanges();
                }

            }
        }
    }
}

1 个解决方案

#1


3  

have you tried adding enabled="true" on the web.config?

你试过在web.config上添加enabled =“true”吗?

<profile inherits="BaniBookWebApp.Code.Core.ProfileCommon" enabled="true">

#1


3  

have you tried adding enabled="true" on the web.config?

你试过在web.config上添加enabled =“true”吗?

<profile inherits="BaniBookWebApp.Code.Core.ProfileCommon" enabled="true">