Re: Dynamic Username and password

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Re: Dynamic Username and password

Juan Pablo Araya
The best solution starts by implementing your own Mapper class, in
which you can dynamically configure the datasource based on App.config
or Web.config, depending on the kind of application you're building.

In the example above I'm configuring the connection through the
InitMapper() method:

        protected static void InitMapper()
        {
            DomSqlMapBuilder builder = new DomSqlMapBuilder();
            XmlDocument sqlMapConfig =
Resources.GetConfigAsXmlDocument("SqlMap.config");
            ((sqlMapConfig["sqlMapConfig"]["database"]["dataSource"]).Attributes[1]).Value
= "Data Source=(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = " +
ConfigurationManager.AppSettings["DataHost"] + ")(PORT = " +
ConfigurationManager.AppSettings["DataPort"] + "))(CONNECT_DATA =
(SERVER = DEDICATED)(SID = " +
ConfigurationManager.AppSettings["DataOSID"] + ")));User ID=" +
ConfigurationManager.AppSettings["DataUser"] + ";Password=" +
ConfigurationManager.AppSettings["DataPass"];
            _Mapper = builder.Configure(sqlMapConfig) as SqlMapper;
        }

My SqlMap.config just has this information related to the datasource

  <database>
    <provider name="oracleClient1.0"/>
    <dataSource name="MyPoolName" connectionString=""/>
  </database>


In this case you get and XmlDocument from SqlMap.config and then
insert the information from my web.config (this sample is for an
oracle connection):

    <add key="DataHost" value="theHost"/>
    <add key="DataPort" value="thePort"/>
    <add key="DataOSID" value="theOSID"/>
    <add key="DataUser" value="myDBuser"/>
    <add key="DataPass" value="myDBPass"/>

Finally, the mapper is build with this information:

_Mapper = builder.Configure(sqlMapConfig) as SqlMapper;

The last thing is use this Mapper instead of the classic
Mapper.Instance()  when you need to persist:

return yourNamespace.Mapper.Instance().Queryfor{........}

In case you use embedded xml, the only difference from the code is in
how you get the XmlDocument (InitMapper() method)

            XmlDocument sqlMapConfig =
Resources.GetEmbeddedResourceAsXmlDocument("sqlMap.config,
your.assemblie");


Greetings!

-----------code------------------

using System.Xml;
using IBatisNet.Common.Utilities;
using IBatisNet.DataMapper;
using IBatisNet.DataMapper.Configuration;
using System.Diagnostics;
using System.Configuration;

namespace yourNamespace
{
    public class Mapper
    {
        private static volatile SqlMapper _Mapper = null;

        protected static void Configure(object obj)
        {
            _Mapper = null;
        }

        protected static void InitMapper()
        {
            DomSqlMapBuilder builder = new DomSqlMapBuilder();
            XmlDocument sqlMapConfig =
Resources.GetConfigAsXmlDocument("SqlMap.config");
            ((sqlMapConfig["sqlMapConfig"]["database"]["dataSource"]).Attributes[1]).Value
= "Data Source=(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = " +
ConfigurationManager.AppSettings["DataHost"] + ")(PORT = " +
ConfigurationManager.AppSettings["DataPort"] + "))(CONNECT_DATA =
(SERVER = DEDICATED)(SID = " +
ConfigurationManager.AppSettings["DataOSID"] + ")));User ID=" +
ConfigurationManager.AppSettings["DataUser"] + ";Password=" +
ConfigurationManager.AppSettings["DataPass"];
            _Mapper = builder.Configure(sqlMapConfig) as SqlMapper;
        }

        public static SqlMapper Instance()
        {
            if (_Mapper == null)
            {
                lock (typeof(SqlMapper))
                {
                    if (_Mapper == null)
                    {
                        InitMapper();
                    }
                }
            }
            return _Mapper;
        }

        public static SqlMapper Get()
        {
            return Instance();
        }


        public static void Clear()
        {
            lock (typeof(SqlMapper))
            {
                _Mapper = null;
            }
        }
    }
}




2009/12/10 sanjeev40084 <[hidden email]>:

>
> Right now my config looks something like this:
>
> Development.config
>
>  <DatabaseConnection
>        name="EmpDb"
>        sqlmapconfigpath="Emp.SqlMap.config"
>        assembly="Emp.Domain"
>  >
>        <DatasourceProperty name="provider" value="sqlServer2.0"/>
>        <DatasourceProperty name="name" value="EmpDb"/>
>        <DatasourceProperty name="server" value="TRIO" />
>        <DatasourceProperty name="initialCatalog" value="Company" />
>        <DatasourceProperty name="username" value="Prince" />
>        <DatasourceProperty name="password" value="persia123" />
>        <DatasourceProperty name="timeout" value="600" />
>        <DatasourceProperty name="caching" value="True" />
>  </DatabaseConnection>
>
> This works fine but only the problem is the username and password is hard
> coded. Anyone who is able to
> use the dll, can view username and password using Reflector. So, i was
> wondering if there was a way where my application (which uses dll which
> contains this connection string), can pass username and password dynamically
> and retrieve the information?
>
>
> --
> View this message in context: http://old.nabble.com/Dynamic-Username-and-password-tp26733588p26733588.html
> Sent from the iBATIS - User - Cs mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>



--
Juan Pablo Araya
787 76 034

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]