Using Windows Authentication with ASP.NET MVC

泪湿孤枕 提交于 2019-12-14 04:16:20

问题


I am sure this is a basic answer, but my search powers are not helping me today. I have an ASP.NET MVC 2 (.NET 3.5) application. It is hosted on IIS 6.

For the sake of this question I have two urls.

http://example.com/

http://example.com/admin

I want admin to be available to any user on the domain, and the root to be available to all users. The server is on the domain but the domain is example1.com, so they are not the same.

Since this is ASP.NET MVC there is no Admin folder to set rights on. I have tried setting the whole site to block anonymous request and have had it allowing all request. Then in the web.config I set it up to deny unauthenticated users in the admin folder and/or allow anonymous access to the root.

What I end up getting is a login prompt that doesn't work. I've tried the user name and example1\username but it always fails to login.

What am I missing?

==== Clarification/Answer ==== Pandincus gave a great code suggestion, adding [Authorize] to the class is a very clean way to make this work. However my problem ended up being the fact that I was doing all of this on the same box. I had a HOSTS entry for example.com on my box and was hitting it via the URL. When I put a HOSTS entry on my other box and hit the site it allowed me to log in without issue. My question was slightly incomplete as I should have mentioned this and the fact that http://localhost/ was working just fine.


回答1:


Don't use the web.config to set access. Thats how you would do it in asp.net web forms.

  1. Configure your web.config to use windows authentication.
  2. Use an [Authorize] attribute on any admin controller you have.

Any controllers without an [Authorize] attribute should be open to the public automatically. So, only put your attribute on the controllers you want to lock down.



来源:https://stackoverflow.com/questions/5343209/using-windows-authentication-with-asp-net-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!