Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • 3ijtKwijt 36 posts 216 karma points
    Aug 23, 2017 @ 09:13
    3ijtKwijt
    0

    Razor membership from web.config

    Hi there

    I was wondering if it is somehow possible to retrieve a certain parameter from a membership provider in the Web.config?

    I've been looking around and found how to get items from appSettings but not this...

    What I basically want to do is fetch the parameter allowedRoles so I can use it in my Razor. Why do I want to fetch it from the Web.config? Because the needed values are already there and doesn't have to be written / changed on multiple places.

    Anyone knows if this is possible, and how

    Thanks in advance!

  • Manish 373 posts 932 karma points
    Aug 23, 2017 @ 10:15
    Manish
    0

    var variable = WebConfigurationManager.AppSettings["mykey"].ToString();

  • Manish 373 posts 932 karma points
    Aug 23, 2017 @ 10:18
    Manish
    0

    var var1 =ConfigurationManager.AppSettings["TestConfig"];

  • 3ijtKwijt 36 posts 216 karma points
    Aug 23, 2017 @ 10:26
    3ijtKwijt
    0

    Hi there,

    Thanks for your reply! I already knew about this snippet as I told in my start post. Even though this isn't exactly what I'm looking for, I've tried this snippet with a newly added line in the appSettings but it didn't seem to be able to find the newly added line.

    Razor:

    @System.Configuration.ConfigurationManager.AppSettings["allowedBackOfficeRoles"]
    

    Web.config appsetting:

    <add key="allowedBackOfficeRoles" value="Role1,Role2,Role3" />
    

    Using the code above, it doesn't show anything, and when adding .ToString() to the Razor code it gives me an error Object reference not set to an instance of an object.

  • Manish 373 posts 932 karma points
    Aug 23, 2017 @ 10:39
    Manish
    0

    @using System.Configuration;

  • 3ijtKwijt 36 posts 216 karma points
    Aug 23, 2017 @ 10:43
    3ijtKwijt
    0

    Sadly that didn't made any changes to the result I had before. Which is kinda obvious as adding @using System.Configuration; wouldn't make any changes because I'm adding it directly in my Razor snippet that I gave.

    It has probably something to do with the way I added it to the Web.config... Do I have to add it somewhere else as well? I don't know, it works with the appSetting values that were already in Web.config but not for the one that I just added.

    I even tried to give the number of the place in the appSettings like this

    @System.Configuration.ConfigurationManager.AppSettings[17]
    

    But that gives me an index out of bounce exception, so I assume it didn't found the needed item.

    Edit: weird... somehow after reopening the project it did work this way.

    • Yet I would still like to being able to access the items from the membership provider to avoid duplicate values and having to change them on multiple places. Is this possible?
  • Manish 373 posts 932 karma points
    Aug 23, 2017 @ 11:09
    Manish
    0

    My suggestion is to create a node in CMS let give it name "Settings" and take all things from there so it would also be easy to access and change.

    enter image description here

  • 3ijtKwijt 36 posts 216 karma points
    Aug 24, 2017 @ 05:10
    3ijtKwijt
    0

    So I assume it's not possible to access the membership providers part from Web.access in Razor

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Aug 24, 2017 @ 16:08
    Dan Diplo
    0

    You can access values of the default membership provider (UmbracoMembershipProvider) via the Membership singleton that should be exposed in Razor views.

    eg.

    @System.Web.Security.Membership.EnablePasswordReset
    

    or

    @System.Web.Security.Membership.MaxInvalidPasswordAttempts
    

    If you want to reference a specific provider (eg. users), then you can access it by name like this:

        @{
            var provider = Membership.Providers["UsersMembershipProvider"];
            <p>@provider.MaxInvalidPasswordAttempts</p>
        }
    
  • 3ijtKwijt 36 posts 216 karma points
    Aug 28, 2017 @ 05:18
    3ijtKwijt
    0

    Sorry for my late reply.

    I had tried something alike earlier which didn't worked, and just tried this as well, but also didn't worked:

    @{
                    var provider = Membership.Providers["BackofficeMembershipProvider"];
                    <p>@provider.allowedRoles</p>
                }
    

    Gives:

    'System.Web.Security.MembershipProvider' does not contain a definition for 'allowedRoles' and no extension method 'allowedRoles' accepting a first argument of type 'System.Web.Security.MembershipProvider' could be found (are you missing a using directive or an assembly reference?)

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Aug 28, 2017 @ 08:40
    Dan Diplo
    0

    I think you must be missing a reference to the System.Web.ApplicationServices.dll Can you add that and try again?

    Some docs...

    https://msdn.microsoft.com/en-us/library/system.web.security.membershipprovider.aspx

    https://msdn.microsoft.com/en-us/library/wkze6zky.aspx

  • 3ijtKwijt 36 posts 216 karma points
    Aug 28, 2017 @ 09:04
    3ijtKwijt
    0

    Thanks,

    Isn't there a possibility to manually download the dll as im using Webmatrix

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Aug 28, 2017 @ 09:10
    Dan Diplo
    0

    Sorry, I'm not sure about how you add references via WebMatrix as I don't use it. I'm sure there must be a way, though.

    You can also download Visual Studio 2017 community edition for free, you know - https://www.visualstudio.com/

  • 3ijtKwijt 36 posts 216 karma points
    Aug 29, 2017 @ 06:34
    3ijtKwijt
    0

    I have tried as you said, sadly still the same issue...

    Might be because it's a custom property on a custom membership provider? I feel like I have to edit the membership provider and add an override get property in the library from it?

  • 3ijtKwijt 36 posts 216 karma points
    Aug 29, 2017 @ 06:53
    3ijtKwijt
    100

    I managed to get it to work as followed:

    @using System.Web.Configuration;
    @using System.Configuration;
    
    @{
          MembershipSection membershipSection = (MembershipSection)WebConfigurationManager.GetSection("system.web/membership");
          ProviderSettings providerSettings = membershipSection.Providers["BackofficeMembershipProvider"];
          string allowedRoles = providerSettings.Parameters["allowedRoles"];
    
          <p>@allowedRoles</p>
    }
    

    Thanks alot for the help though :)

Please Sign in or register to post replies

Write your reply to:

Draft