Results 1 to 10 of 10

Thread: Possible to create a web.config and new role via code

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618

    Possible to create a web.config and new role via code

    I have a system where different users can upload files. Internally to the system I have administrators and regular users who can upload files into directories protected with web.config files and appropriate roles. However I have the need on the fly when a new customer account is created to create a directory that only that customer (and internal users) can use. I "think" as part of the customer creation process I could create a directory and role based on the customerID, and then use that info to write a web.config file that grants access to the new role and secure the directory. Is this possible? Has anybody done anything like this?

    Thanks!

    Sean
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

  2. #2
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    Re: Possible to create a web.config and new role via code

    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618

    Re: Possible to create a web.config and new role via code

    Quote Originally Posted by sapator View Post
    Hmm thanks for the reply I guess I asked it before and then got sidelined. Not quite what I want to do. I guess let me try what I want to do and see if it works.
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618

    Re: Possible to create a web.config and new role via code

    This is kind of where was going with this where intCustomerID as an index to a new row in a Customer table in SQL.

    Code:
        Public Function TestWriteWebConfig(ByVal intCustomerID As Int32) As Boolean
    
            Dim strWebConfig As String = m_strSavePath & "\Customer\" & intCustomerID.ToString() & "\web.config"
    
            Dim fs As New FileStream(HttpContext.Current.Server.MapPath(strWebConfig), FileMode.Create, FileAccess.Write)
            Dim sw As New StreamWriter(fs)
    
            Dim strRole As String = intCustomerID.ToString() & "_Customer"
    
            Roles.CreateRole(strRole)
    
            sw.WriteLine("<?xml version=""1.0""?>")
            sw.WriteLine("<configuration>")
            sw.WriteLine("")
            sw.WriteLine("<system.web>")
            sw.WriteLine("<authorization>")
            sw.WriteLine("<allow roles=""" & strRole & """/>")
            sw.WriteLine("<allow roles=""ACS""/>")
            sw.WriteLine("<allow roles=""ACS_Administrators""/>")
            sw.WriteLine("<deny users=""*""/>")
            sw.WriteLine("</authorization>")
            sw.WriteLine("</system.web>")
            sw.WriteLine("")
            sw.WriteLine("</configuration>")
    
            sw.Close()
            fs.Close()
    
            Return True
        End Function
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

  5. #5
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    Re: Possible to create a web.config and new role via code

    Hi,
    How about just adding an items to web.config file rather than
    creating a new one? There might be some scenarios that your web.config
    has other elements as well.

    how-do-you-modify-the-web-config-appsettings-at-runtime

    KG
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  6. #6
    Addicted Member masoudk1990's Avatar
    Join Date
    Nov 2013
    Location
    Persia
    Posts
    172

    Re: Possible to create a web.config and new role via code

    I'm not sure about modifying web.config via code, but I've experienced "File is in use" error when i tried to modify a xml file via code and result was a failure.

    If you want to modify web.config with stream writer before deployment be sure you don't get "File is in use" error.
    Computer Enterprise Masoud Keshavarz
    For more information contact masoudk1990@yahoo.com

  7. #7
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    Re: Possible to create a web.config and new role via code

    "Playing" with the root web.config, if doable, when the site is live is a very bad idea. One mistake in code and, BOOM, your website is down. Also this will require a veeeeeery long config file if you treat many users and directories, that is bad for the readability..
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  8. #8
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Possible to create a web.config and new role via code

    Not only that, but the changes may not get picked up right away. I know when I make changes to my local web.config, I have to reset the app pool before the changes get picked up.

    surely there is a better way to handle this than through the web.config file.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    Re: Possible to create a web.config and new role via code

    surely there is a better way to handle this than through the web.config file.
    Perhaps a separate XML file..

    KG
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  10. #10
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Possible to create a web.config and new role via code

    Quote Originally Posted by KGComputers View Post
    Perhaps a separate XML file..

    KG
    Except that the reason to have it in the web.config in the first place is that then IIS will control the access accordingly. Dumping it into a different file would mean self-implementation, which sounds like it would be bigger deal to over come.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width