I want to disable requiring authentication for one MVC controller action - c#

I have created an mvc application I don't want authentication for one functionality in my application. I want that the user should able to open the page from the URL without log in.Right now if I enter the URL to open that functionality directly then it will take me to the sign on page. I want to bypass the authentication process in one functionality.
I have tried following code in my web.config.
<location path="ControllerName">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</location>
And I have also added [AllowAnonymous] attribute to the some action of my controller.
The following code that i have written for authentication in my web.config.
<modules runAllManagedModulesForAllRequests="true">
<remove name="FormsAuthentication"/>
<add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule"/>
<remove name="AuthorisationModule"/>
<add name="AuthorisationModule" type="ProjectName.AuthorisationModule(which is for autentication), Project Name"/>
</modules>

There is an authentication for the whole application and I want discard the authentication of just one feature of my mvc application. So I have added the following code in my web.config
<location path="ControllerName">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</location>
So using this code it's working fine.

Something that may help with your issue is the fact that you can have multiple web.config files -- if you put a web.config in a subdirectory within your project, then you can include configuration options specific only to that subdirectory. As such, you may want to try adding a subfolder to contain just the controller which you want to make publicly available, and then creating a separate web.config file allowing open access to that subdirectory. As per this answer regarding wildcards in web.config files, here is example code that should suffice as a standalone web.config to provide
<?xml version="1.0"?>
<configuration>
<system.web>
<authorization>
<deny users="*"/>
</authorization>
</system.web>
</configuration>
That way you don't have to mess around with specifying the one controller or action you want to give access to, as the distinction is made by the organization of the files in the directory structure.

Related

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

Config IIS or Web.config to serve all files in a specific folder only

I am developing a CDN server that is a normal MVC project. However, in the project folder, suppose I have CDN folder, and I want everyone are allowed to access any files in there, with any file extension, and for CDN folder only. That is, I don't want my web.config file or Views folder for example to be exposed.
Is there any way to do this without using a custom Controller or RouteHandler?
EDIT: As specified I am looking for a way in IIS or Web.config or somewhere else of writing code in Controller or RouteHandler, so it is not a duplicate.
Following David Gunderson answer, here is my web.config that allow CDN folder:
<location path="CDN">
<system.webServer>
<staticContent>
<mimeMap fileExtension="*" mimeType="application/octet-stream"/>
</staticContent>
</system.webServer>
</location>
You can configure location settings for specific sub directories of your site in your web config:
https://msdn.microsoft.com/en-us/library/ms178692%28v=vs.140%29.aspx
The following would grant all users access to the contents of the CDN folder if placed in your root web.config. It will grant access to anonymous users even if you have forms authentication turned on for the rest of your site:
<location path="CDN">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</location>

ASP.NET WebForms Url authorization and an infinite loop. Web.config not working?

I've created a website which uses ASP.NET Identity for user account functionality.
I want to restrict access to all pages in a specific folder ("Account") except "Login" and "Register" in my application using standard url authorization.
Not logged-in users should be able to open only "Account/Login" and "Account/Register" and those authenticated should be able to open everything else except those pages.
The root Web.config has no authorization rules and a Web.config which I put in the Account folder has that:
<?xml version="1.0"?>
<configuration>
<system.web>
<authorization>
<deny users="?"/>
</authorization>
</system.web>
<location path="Login.aspx">
<system.web>
<authorization>
<allow users="?"/>
<deny users="*"/>
</authorization>
</system.web>
</location>
<location path="Register.aspx">
<system.web>
<authorization>
<allow users="?"/>
<deny users="*"/>
</authorization>
</system.web>
</location>
</configuration>
With those rules, however, there is a problem. Requesting any page in that folder, including "Login.aspx", causes a redirection to http://localhost:15284/Account/Login. As I said, even requesting the "Login" page redirects back to itself just like the user wasn't allowed so an infinite loop is created. That loop causes HTTP 404.15, because the query string exceeds its length limit ("?ReturnUrl=%2FAccount%2FLogin" is appended to the URL on every redirection).
Are my rules incorrect or this is something else?
Perhaps the problem is somehow related to ASP.NET Identity?
Or maybe this is happening because of url rewriting (enabled by default in VS 2013 WebForms template)?
Without that Web.config the website of course works but everyone has access to everything which is not really something I want.
Thanks in advance and sorry for my English! :)
Try
<location path="Login.aspx">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</location>
This should allow access to unauthenticated users
Use the folder "account", or create another one in which may insert the pages that you want accessible from anonymous user, with its owner web.config that contain the following configuration:
<configuration>
<system.web>
<authorization>
<allow users="?"/>
</authorization>
</system.web>
</configuration>
As you can see without using "location" tag
<location path="Login.aspx">
whereas in the root web.config in which it remains this:
<authorization>
<deny users="?"/>
</authorization>
This is a workaround because as explained in this article: http://msdn.microsoft.com/en-us/library/wce3kxhd.aspx
"Rules contained in application-level configuration files take precedence over inherited rules.
The system determines which rule takes precedence by constructing a merged list of all rules for a URL, with the most recent rules (those nearest in the hierarchy) at the head of the list"
A bit old thread, but I hope this helps, at least someone.
The redirect to /Account/Login comes from Startup.cs, which by default is somewhat the following:
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login")
});
}
}
So, replace the LoginPath = new PathString("/Account/Login") with your login path.
I struggled with the same problem for a while, but it works now..

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>

Location in web.config of secured pages

I am developing a Web Applilcation in Asp.net 4.0 , wherein i have two types of Pages "Secured" and "Unsecured".
To access Secure Pages, am using Location tag in my secured's web.config and the user must successfully Login. and after that login am going to copy that url and then logout.
My expected result:- while am going to paste that url in browser then it will again display me the login page again.
so please give me the example related to my query.
Thanks In Advance.....
You need to use the authorization tag in your location to restrict access for anonymous users e.g.
<location path="Secured.aspx">
<system.web>
<authorization>
<deny users="?"/>
</authorization>
</system.web>
</location>
What I do is create a folder (perhaps named 'Secure') and add a web.config file inside the folder. It will be very small... possibly as simple as this (which only permits logged-in users):
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<authorization>
<deny users="?" />
</authorization>
</system.web>
</configuration>
Any page added to the folder will cause a login prompt upon access without the need to do anything else on your part. This is assuming you are using ASP.Net Membership/Security features (as it sounds like from your question).
Note: the additional/small web.config file will be limited to the scope of the folder that contains it.
If you wanted to create a couple roles (we do this for our Internal Admin pages), you can restrict access to only users tied to those roles... it's pretty easy. The following web.config is in our Admin folder that contains all our admin pages...
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<authorization>
<allow roles="FullClearance" />
<allow roles="HighClearance" /><!-- mgr clearance-->
<allow roles="StandardClearance" /> <!-- staff clearance-->
<deny users="*" /><!-- authenticated users -->
<deny users="?" /><!-- anonymous users -->
</authorization>
</system.web>
</configuration>

Categories

Resources