简体   繁体   中英

Please help! How to specify Dynamic Properties for ASP.NET Profiles

My requirement is to create a framework that can consume standard ASP.NET Profile provider. The application contains two libraries, one for Profile access (saving and retrieving Profile using a ProfileBase class) – A framework class library, and the other one for defining properties for the Profile – Developer client class library which consumes the above framework class library.

The idea here is that the developers don't need to know about the underlying profile implementation (in the framework class library), and all they have to do is to provide properties in a class so the profile can be set and get as expected.

My implementation is below. (Please note that I have configured the authentication, connection strings and role providers successfully)

Web.config->

   <profile inherits="MyCompany.FrameworkLib.ProfileSettingsService, MyCompany.FrameworkLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=12ac5ebb7ed144" >
<providers>
    <clear/>
        <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />
    </providers>
</profile>

   *Our design is not to specify profile properties in the web.config.

Implementation of MyCompany.FrameworkLib.ProfileSettingsService ->

public class ProfileSettingsService : ProfileBase, IProfileSettingsService
{
     "**Note that if I un-comment the below code Profile works as expected. I do not want this property to be here, but somehow discover it dynamically based on the values being passed to this class. How can I do this?.**"

    //[SettingsAllowAnonymous(false)]
    //public string HideTransactionButton
    //{
    //    get { return base["HideTransactionButton"] as string; }
    //    set { base["HideTransactionButton"] = value; }
    //}

    #region IProfileSettingsService Members

    public T Get<T>(string key, T defaultValue)
    {
        if (string.IsNullOrEmpty(key))
        {
            throw new ArgumentNullException("key");
        }

        object profileValue = null;

        try
        {
            profileValue = GetUserProfile().GetPropertyValue(key);
        }
        catch { }

        if (profileValue is T)
        {
            return (T)profileValue;
        }

        return defaultValue;            
    }


    public void Set<T>(string key, T value)
    {
        GetUserProfile().SetPropertyValue(key, value);
        GetUserProfile().Save();
    }

    public ProfileSettingsService GetUserProfile(string username)
    {
        return Create(username) as ProfileSettingsService;
    }


    public ProfileSettingsService GetUserProfile()
    {
        var userName = HttpContext.User.Identity.Name;

        if (userName != null)
        {
            return Create(userName) as ProfileSettingsService;
        }

        return null;
    }
    #endregion
}

implementation of MyCompany.ConsumeLib.ProfileContext ->

public class ProfileContext : IProfileContext
{
    #region IProfileContext Members

    [Dependency] //Note that I use Unity for DI
    public IProfileSettingsService ProfileSettingsService { get; set; }

    public string HideTransactionButton
    {
        get { return this.ProfileSettingsService.Get<string>("HideTransactionButton", "false"); }
        set { this.ProfileSettingsService.Set("HideTransactionButton", value); }
    }

    #endregion

}

So the question is how to get the Profile working without having to uncomment

//[SettingsAllowAnonymous(false)]
//public string HideTransactionButton
//{
//    get { return base["HideTransactionButton"] as string; }
//    set { base["HideTransactionButton"] = value; }
//}

in MyCompany.FrameworkLib.ProfileSettingsService

I need to be able to discover properties dynamically within ProfileSettingsService without having to explicitly specifying properties. In this way, developer doesn't need to worry about maintaining properties in two libraries - (one in the frameworkLib, and the other one is in the ConsumeLib.)

Any ideas greatly appreciated.

Visit this article, can help you:

http://msdn.microsoft.com/en-us/magazine/cc163724.aspx

also this page in MSDN

Defining ASP.NET Profile Properties

I have decided to take another approach to solver this problem. Unfortunately the way MS has designed the ProfileBase class there is no easy way to do this.

There are 2 possible solutions. I use below 'b' a. Add profile properties to Web.config and use T4 templates dynamically generate them. b. Have a look at the http://archive.msdn.microsoft.com/WebProfileBuilder . This will also dynamically generate profile properties at compile time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM