How to configure access by host-name using web.config - c#

I am binding web app with 2 host names, example:
abc.com
xyz.com
I have a page t1.html. I would like to set permission access on file
abc.com/t1.html → allow
xyz.com/t1.html → not allow to access
I guess we can do it in web.config not in code.
I did some research but most of the posts are related to authorization which doesn't help me. For example:
<location path="your_directory">
<system.web>
<authorization>
<deny users="?"/>
</authorization>
</system.web>
</location>

Take a look at the rewrite module, one of the possible actions is abort, or you could redirect traffic to a different url based on the hostname.

Related

How to check if a user has access to a specific file in ASP.NET [duplicate]

I'm building a dynamic navigation control which toggles the visiblity of elements in the navigation depending on which pages the user is authorized to view in the web.config.
To find out if a user is allowed to visit a page, I use the CheckUrlAccessForPrincipal method and set the authorization rules for a whole directory like this.
<?xml version="1.0"?>
<configuration>
<system.web>
<authorization>
<allow roles="demo\Administrators"/>
<deny users="*" />
</authorization>
</system.web>
</configuration>
This works just as expected and CheckUrlAccessForPrincipal returns false for all pages in the directory that contains the web.config-file when the current user is not in the Administrators group.
Now I want to set authorization rules on a page level like this.
<?xml version="1.0"?>
<configuration>
<location path="DemoPage.aspx">
<system.web>
<authorization>
<allow roles="demo\SomeDifferentGroup"/>
<deny users="*" />
</authorization>
</system.web>
</location>
</configuration>
If I now check whether a user is allowed to access DemoPage, CheckUrlAccessForPrincipal returns true, no matter if the current user is in SomeDifferentGroup or not.
I have assured that the configuration is correct. Visiting a page for which the current user is not authorized opens the Windows-Authentication and prompts the user to provide valid credentials.
Any ideas why the behavior differs depending on whether authorization-rules are set on a directory or folder level?
Ok so after 2 days of digging I finally found the answer.
Apparently, some ASP.NET projects omit the .aspx file-ending in the URL.
If one now were to call the CheckUrlAccessForPrincipal method with a URL that is missing the .aspx ending, the method will somehow not recognize correctly that the URL is a page and not check the web.config authorization rules correctly.
Manually adding the file-ending to the URL has fixed the problem.

How to allow only a specific set of web pages accessible over internet using IIS?

I have a website with 10 web pages(aspx). I want to allow only 5 pages accessible over internet and all other over Intranet. I am using IIS 8. Is there a way with out using any security or Login features.
At IIS level you would could split out the sites into public and private with bindings on different IP addresses. You could then use firewall rules or maybe IIS IP restrictions to achieve this (set IIS to grant access to specific IP address mask)
However it can be fairly easy to setup in your application by using the <location> element in the web.config to handle separate authentication and authorization schemes for different pages. Make sure you have windows auth setup as per https://stackoverflow.com/a/360717/1165140
<configuration>
<location path="Internet.aspx">
<system.web>
<authorization>
<allow users="?"/>
</authorization>
</system.web>
</location>
<location path="Intranet.aspx">
<system.web>
<authentication mode="Windows"/>
<authorization>
<allow users="?" />
</authorization>
</system.web>
</location>
</configuration>

How to use Authorization & Authentication in Asp.net, C#?

I am using Roll management and I am trying to give page and folder access according to user or user group, Also using server created AD group for user authentication.
I have default1.aspx page as default and subdir1 folder to give different access for separate user group
I am using below logic in web.config.
<location path="subdir1">
<system.web>
<authorization>
<allow users ="?" />
</authorization>
</system.web>
</location>
I am facing problem to provide same access to 2 or more directory to same user so should I have to provide allow user code twice for both folder?
I can use this logic by repeating value for all folder but I want to do all access providing in one logic.
I have got the answer to configure folder/page access, For that i have to make different access as shown below..
Configure Access to a Specific File and Folder, Set up forms-based authentication.
Request any page in application to be redirected to Logon.aspx automatically.
In the Web.config file, done the following code.
This code grants all users access to the Default1.aspx page and the Subdir1 folder.
<configuration>
<system.web>
<authentication mode="Forms" >
<forms loginUrl="login.aspx" name=".ASPNETAUTH" protection="None" path="/" timeout="20" >
</forms>
</authentication>
<!-- This section denies access to all files in this application except for those that you have not explicitly specified by using another setting. -->
<authorization>
<deny users="?" />
</authorization>
</system.web>
<!-- This section gives the unauthenticated user access to the Default1.aspx page only. It is located in the same folder as this configuration file. -->
<location path="default1.aspx">
<system.web>
<authorization>
<allow users ="*" />
</authorization>
</system.web>
</location>
<!-- This section gives the unauthenticated user access to all of the files that are stored in the Subdir1 folder. -->
<location path="subdir1">
<system.web>
<authorization>
<allow users ="*" />
</authorization>
</system.web>
</location>
</configuration>
Users can open the Default1.aspx file or any other file saved in the Subdir1 folder in your application. They will not be redirected automatically to the Logon.aspx file for authentication.
Repeat configuration Step to identify any other pages or folders for which you want to permit access by unauthenticated users.
For more Reference check Microsoft support page - https://support.microsoft.com/en-us/kb/301240
And also you can check http://www.iis.net/configreference/system.webserver/security/authorization
After you have to do coding on login page for reference check this -> http://www.codeproject.com/Articles/13872/Form-authentication-and-authorization-in-ASP-NET
Actually, the asp.net user access management spans widely so I've decided to introduce you two links which help me a lot. Hope this could help you as well.
Understanding Role Management
Walk through role management

First page of my website doesn't load correctly "css"

I have a problem with my website using C# and .Net.
When i try to access to my website the first page doesn't load correctly because of the css which is the authentication page.
But after the connection, the website work perfectly by miracle and the css load correctly...
Is any one can help me please with this i would be very grateful.
This is a common problem. I'm assuming you're using Forms Authentication, right?
Your login page has a reference to your css file. Let's say your css file is at /css/site.css. When a user isn't authenticated, then their browser is denied access to the css file that your login page needs to render properly.
You need to configure your website to allow unauthenticated access to that path of your application. In your web.config, add the following inside your <configuration> tag.
<location path="css/site.css">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>
This is bacause the CSS file needs authtication but at the stage of login page, the authentication is not yet available.
You need to allow unauthenticated access to the css file.
Add the following into the "" tag inside web.config file.
<location path="Path/file.ext">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</location>

Web.config Location element functioning incorrectly

Environment: ASP.NET 3.5, C#, Forms Authentication, IIS 6
Problem details: I have a web.config file set up with forms authentication and the following are the location element, as appearing:
<location path="Home/Common">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>
<location path="Business/Services">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>
The Home/Common folder contains my ForgotPassword.aspx.
The Login.aspx page is set as the LoginUrl
When the Login page is visited the first time (with no prior cookies etc.) the ForgotPassword link functions fine. It redirects to the page as desired.
However, once a user has logged in, upon Sign Out, the ForgotPassword link doesnot redirect. Rather, forms authentication precedes and redirects to the login url with the ReturnURL querystring pointing to Forgot Password page.
The question simply is: If prior to signing in the element is considered and rightly excluded from forms authentication, why post Signing in and Sign out it gets in the purview of Forms authentication.
It is to be noted that on clearing Browser history, the functionality works as expected.
Any help would be appreciated.
Thanks.
It is more common to use the question mark (?) to allow/deny unauthenticated users. Unauthenticated users are the ones that really need to use the login page and password reset functionality, so allowing all users (*) to access them is an incorrect configuration. However, you have not posted your entire Web.config. There will be a root configuration for authorization that deals with "everything else."
How I would likely configure this is within the root <system.web>, I'd have:
<authorization>
<allow users="*" />
</authorization>
And later in the Web.config, define locations that are secured:
<location path="Business/Services">
<deny users="?" />
</location>
Which denies all unauthenticated users to pages within that folder. Your login and forgot password pages would be in the root folder. Regardless, either I'm missing something or you do not have the root authentication configured which might be confusing ASP.NET's authentication.

Categories

Resources