locked
How to pass userid and Password in Web.config got from session variable RRS feed

  • Question

  • User337566521 posted

    Hi ,

     

    I have got a requirement to pass password and UseriD to web.config placed in a session variable . How can I do this? Please tell me as its very urgent

    in VB file it is written in this way. So, I have to pass it in Web.congif the Session variable please give me a code or solution

    dataSource.ConnectionString = "Provider=MSDAORA.1;Password=ssa_nic_" & Session("dist_code") & ";User ID=dise" & Session("dist_code") & ";Data Source=dise;Persist Security Info=True"

    Saturday, March 19, 2011 3:05 AM

Answers

  • User3866881 posted

    In your asp.net, you should config like this:

     <connectionStrings>
    <add name="NorthwindEntities" connectionString="Server=.;database=YourDatabaseName;integrated security=sspi" />
    </connectionStrings>

    If you are using V2008 or VS2010 (3.5), you can see connectionString tag at:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <configSections>
        <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
          <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
            <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
            <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
              <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere" />
              <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
              <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
              <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
            </sectionGroup>
          </sectionGroup>
        </sectionGroup>
      </configSections>
      <appSettings />
      <connectionStrings>
       ……………………  

    </connectionStrings>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 28, 2011 9:04 PM

All replies

  • User814673218 posted

    This is what you need to do. Full details are provided in the below article.

    http://www.eggheadcafe.com/articles/20030907.asp

    Remember, editing and saving the web.config will restart the application.

    Saturday, March 19, 2011 8:04 AM
  • User-1227788583 posted

    Hi bammidi,

    Use reflection to access non-public members.

    var settings = ConfigurationManager.ConnectionStrings[ 0 ];
    
    var fi = typeof( ConfigurationElement ).GetField( "_bReadOnly", BindingFlags.Instance | BindingFlags.NonPublic );
    
    fi.SetValue(settings, false);
    
    settings.ConnectionString = "Data Source=Something";
    Check out this link: http://david.gardiner.net.au/2008/09/programmatically-setting.html

    Saturday, March 19, 2011 8:53 AM
  • User337566521 posted

    Hi Naveen,

    Thanks for your reponse. Well can you please provide me the code, as per mine to place it in Web.config along with the Session variables in the connectionstring using reflection. This would be very thankful to me.

     

     

    Sunday, March 20, 2011 1:36 PM
  • User3866881 posted

    Hi:)

    If you have a webconfig with a node like this:

     <connectionStrings>
        <add name="NorthwindEntities" connectionString="…… />
      </connectionStrings>

    Now let's say this in the Page_Load event in the body of if(!IsPostBack){……}

     ConfigurationManager.ConnectionStrings["NorthwindEntities"].ConnectionString = Here please put your connection with Session combination;

    Monday, March 21, 2011 10:02 PM
  • User337566521 posted

    Hi 

     

    Thank you for your reply but here , how can I pass the userID and paasowrd to web.config, as I am connectig to different Databases with the session of Username and Password which i could get. And also I have to write it in Web.config file. Please tell me..........

    Sunday, March 27, 2011 1:02 PM
  • User3866881 posted

    In the some place suitable, please say this:

    ConfigurationManager.ConnectionStrings["NorthwindEntities"].ConnectionString="Server=.;database=Northwind;User Id="+Session["UserName"].ToString()+";Password="+Session["Password"].ToString();

    Sunday, March 27, 2011 9:06 PM
  • User337566521 posted
    Thank you very much for your immediate reply . One thing needed what to write in the web.config file connectionstring
    Monday, March 28, 2011 10:46 AM
  • User3866881 posted

    In your asp.net, you should config like this:

     <connectionStrings>
    <add name="NorthwindEntities" connectionString="Server=.;database=YourDatabaseName;integrated security=sspi" />
    </connectionStrings>

    If you are using V2008 or VS2010 (3.5), you can see connectionString tag at:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <configSections>
        <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
          <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
            <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
            <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
              <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere" />
              <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
              <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
              <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" />
            </sectionGroup>
          </sectionGroup>
        </sectionGroup>
      </configSections>
      <appSettings />
      <connectionStrings>
       ……………………  

    </connectionStrings>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 28, 2011 9:04 PM