Authorized File access in ASP.NET Web Application - c#

Can some one please help me to get an idea on this? I have a C# website application in which I want to do authorization for accessing the documents in website directory.
If user requests for a document say pdf through a link in my website, http://www.mywebapp.com/documents/test.pdf , before opening the test.pdf in browser, I actually want to verify the user is authorized to access the pdf based on role he got. I have enabled forms authentication for the folder "documents" in IIS and system is redirecting to login page if user is not authenticated. I'm all good with that, but stuck with authorization.
I can't set the roles in web.config since it would different for different users. User role is stored in httpcookie for that particular user.
And in my documents folder there would be different documents targeted for different roles.
Say test.pdf for role called vendor. So only vendors can access this pdf
Another document form.pdf for role supplier- only users with role supplier can see this pdf .
Should I write some handler to execute before loading the pdf in browser?
Or when ever requests comes as /documents/ should I have a URL rewrite to execute an aspx page to verify the authorization and if authorized display the page?
Can anybody please help me to get an idea on how to implement this authorization.
Appreciate your help!
Thanks,
KK

Looks like your question is "how I can check cookie value on my page and return stream of a file with correct document type when cookie is ok".
make sure you are handling all request (much easier to do using MVC than WinForms, but possible in later too)
read and verify cookie
return file if check passed, don't forget to set "content-diposition" and "content-type" headers. Again File result in MVC is easier to use... Make sure to read file content under correct account if using impersonation.

You can add following code to web.config and try
<location path="documents/test.pdf ">
<system.web>
<authorization>
<allow roles="Vendors"/>
<deny users="*"/>
</authorization>
</system.web>
</location>
<location path="documents/form.pdf ">
<system.web>
<authorization>
<allow roles="Role Supplier"/>
<deny users="*"/>
</authorization>
</system.web>
</location>

Related

How do I prevent a web form from being accessed by manually typing the aspx file name into the browser?

I have created a Login page that is only for admins (so there is no need to check if the user is admin or not). The page does the check against a database where the username and password is stored. I am able to successfully check the name/password against the database and then transfer the user to the admin page, however, I noticed that I can still type http://localhost:xxxxx/AdminPage and it will take me there. How do I stop people from doing this? I have tried adding
<authorization><deny users="?"/></authorization>
to the web.config file, which stops all anonymous users from entering, but now I need to know how to allow users from the database to be given permission. (NOTE: this is not a normal Login to Default setup. This is directing to a page other than Default)
You can user role based authorization described here.
If you use default ASP role system, you need to add some code to web.config:
<location path="your_page" >
<system.web>
<authorization>
<deny users="?"/>
<allow roles="Admin"/>
</authorization>
</system.web>
</location>

WCF Rest service authentication in web config file

I am developing a WCFRest service and want to authenticate it by a single userid and password .
Go through the lot of stuff on internet .But the thing is complex.
I want to know is there any way to give the permission for single userid,password in web Config file .
<Location path="Test.svc">
<system.web>
<authorization>
<denyusers="?"/>
</authorization>
</system.web>
</Location
If there is any way to authenticate it .Please Help me on this.
Here's the thing: you do not want to just allow a specific USER, you want to allow a specific ROLE, i.e. someone who is allowed to do that. Using a userID and Password means that if you ever need to change who is allowed (because that person has been fired, or because there are now 2 people with that permission), you would have to change the web.config and redeploy the file, which in extreme cases might break the site.
Instead, implement an entire membership system, including roles, and then do this:
<Location path="Test.svc">
<system.web>
<authorization>
<allow roles="[RoleNameHere]">
<deny users="*"/>
</authorization>
</system.web>
</Location>
You can then add the user that you want to give access to that role through the standard way of doing this for your membership provider, and s/he'll automatically be allowed access, while everyone else is denied. You can also easily change whoever is allowed or denied, because it's a data change, not a code change.

How do I use AD Authentication in ASP.NET?

I want to know how to use Active Directory in the Account/Login.aspx page in my project but I cannot find a lot of resources out there for this. I am using VS.Net 2013 Asp.net C# 4.0. I have never had to do this before and I was just wondering how you access Active Directory and on a group level so only the person(s) that are in this group have access to the application. Please anyone with a link or any information that would be great. I am really stuck on this and I need to be able to have this working.
I use active directory and forms authentication. You can use this with the default Account/Login.aspx. These are the links that I have used to set it up.
This link will show you how to log in with Active Directory.
This link will show you how to set up an ADRoleProvider.
I prefer to use them both because the first link will show you how to get the AD groups of the logged in user, but it saves them to an authentication cookie. You will have to decrypt this cookie to see which group the ad user is in... this can be a hassle.
The 2nd link will show you have to use roles, which is much simpler.
You can have a simple if statement like..
if(User.IsUserInRole("SoftDev"))
{
//do something
}
or you use the roles in the web config like this..
<location path="Account/Whatever.aspx">
<system.web>
<authorization>
<allow roles="SoftDev"/>
<deny users="*" />
</authorization>
</system.web>
</location>
this will deny everyone to that page besides the group "SoftDev"
Both these links are meant if you are using Forms Authentication as there are other articles out there for using Windows Authentication. I hope this helps.

Need to login to view video asp.net

I am a beginner with C# and Asp.net. I will try to explain this as best I can. I work at a university that allows access to a lot of different sites, payroll, hr, databases...etc. through a central authentication unit. You login on a subdomain of the site and based on your account, you have access to whatever is allowed to you.
I am working with a small department that wants a video on their website, but they want employees to have to login first, in order to see that video.
Can anyone recommend on how to achieve this, I am kind of dead in the water here. Any help is appreciated. Is there a way I can use asp.net to check if they are logged in? Or is this beyond my hands.
To deny access to a specific page say: video.aspx, you can use location element in your web.config. This will always deny access to users who are not logged in and will redirect them to your default login page configured.
<configuration>
<location path="root/video.aspx"> //Specify your correct Path to Video Page
<system.web>
<authorization>
<deny users="?"/>
</authorization>
</system.web>
</location>
</configuration>
However, if you want authentication on whole of a section of website, which will be a separate folder in VisualStudion, make sure in the root of this separate folder, you place a web.config with below entry. Only thing that your code to validate user in login page will be using the Central authentication standards.
<system.web>
<authentication mode="Forms">
<forms loginUrl="login.aspx" />
</authentication>
<authorization>
<deny users="?" />
</authorization>
</system.web>
You can refer this URL for a very basic startup: http://www.codeproject.com/Articles/13872/Form-authentication-and-authorization-in-ASP-NET

Authorizing a single aspx page with log in

I Have an ASP.net application where I have a page named foo.aspx where some secured data is placed.
I want to denny the access to this page to users not logged in, and this login Username and Password must set by me in web.config or somewhere else.
But I have a problem that there is already a Admin Panel which is restricted to normal users
by Username , Password I have set in web.config using authentication mode set to forms.
Now how can I restrict foo.aspx page as authentication code can't duplicate and also want separate log in page.
In web.config, you could define elements (after system.web closure) where you set a specific configuration for a specific zone/page of your app.
Eg:
<configuration>
<system.web>
....
</system.web>
<location path="Foo.aspx">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>
</configuration>
There, you may set a different authorization mode than the one in use by your app.
With the above example, your page won't be protected by the standard authorization mode configured globally, but you have the choiche to protect your page in a custom way, directly in the page itself.
At the top of your foo.aspx page you have
if(login == false)
send them to login page;
This will make sure that they are logged in before they can even access the foo page.
The best way to achieve this is to have two different asp.net applications. You can still configure your IIS to have your Foo application in another folder / subdomain.
You can, however, try to develop your own AuthenticationModule which will need to have a list of urls protected by Admin-authent and a list of urls protected by Foo-authent, each one with its specific Login page and a default login-pwd pair or a table of users. Here's a tutorial for developing custom Authent module : http://www.codeproject.com/Articles/5353/Custom-Authentication-provider-by-implementing-IHt
Good Luck
You can add your page foo.aspx to a folder and you can protect this folder by adding a new web.config file
for example ,
Folder
-foo.aspx
-web.config
in new web.config file
<?xml version="1.0" encoding="utf-8"?><configuration>
<system.web>
<authorization>
<deny users="?" />
</authorization>
</system.web>
</configuration>

Categories

Resources