ASP.NET MVC - HTTP 身份验证提示
是否可以让我的应用程序在渲染视图之前要求输入用户名和密码? 就像在 Twitter API 上获取有关您帐户的信息一样:
http://twitter.com/account/verify_credentials.xml
所以在渲染视图之前||文件它要求您插入用户名和密码,我认为这是直接在服务器上进行的,因为curl请求基于用户名:密码以及如下所示:
curl -u user:password http://twitter.com/account/verify_credentials.xml
当我尝试构建一个遵循相同结构的API时,我会我想知道如何在 ASP.NET MVC C# 上执行此操作。我已经在 ruby Rails 上使用过它,它非常简单:
before_filter :authenticate
def authenticate
authenticate_or_request_with_http_basic do |username, password|
username == "foo" && password == "bar"
end
我不认为 [Authorize] 过滤器是相同的,因为我相信它只是一个重定向, 它会将您重定向到基于帐户数据库的帐户内部控制器,在这种情况下,我将使用另一个数据库,特别是来自网络服务的数据库,并在提交信息后进行验证。 但我需要采取行动来要求用户并根据其请求传递凭据。
提前感谢
更新:
实际上是请求一个需要此身份验证的页面(即 Twitter) 我必须根据其要求声明这一点
request.Credentials = new NetworkCredential("username", "password");
,这将反映提示的用户名和密码。
因此,这是完全相同的事情,但从另一方面来看,如果可以根据请求向身份验证提示提供信息,我如何要求对请求进行此身份验证?
因此,每当有人尝试向我的应用程序发出请求时,例如:
http://myapplication/clients/verify_credentials
它应该使用该服务器提示符询问用户名和密码 例如,要检索有关卷曲的信息,就会像这样
curl -u user:password http://myapplication/clients/verify_credentials
Is it possible to make my application ask for username and password prompting for it before render a view?
Just like on twitter API to get information about your account:
http://twitter.com/account/verify_credentials.xml
So before render the view || file it asks you to insert you username and password, I think this is made directly on the server since the curl request is based on username:password as well like this:
curl -u user:password http://twitter.com/account/verify_credentials.xml
As I'm trying to build an API following the same structure I would like to know how I can do this on ASP.NET MVC C#. I've already used this on ruby rails and its pretty simple like:
before_filter :authenticate
def authenticate
authenticate_or_request_with_http_basic do |username, password|
username == "foo" && password == "bar"
end
I don't think that [Authorize] filter is the same since I believe it's just a redirection,
and it redirects you to the Accounts Internal Controller that is based on the accounts database, in this case I will use another database, specifically from a webservice and do the validation after the information is submitted.
But I need the action to require the user and pass credentials on its request.
Thanks in advance
UPDATE:
Actually to request a page that requires this authentication (i.e. Twitter)
I would have to declare this on its request
request.Credentials = new NetworkCredential("username", "password");
And this would reflect that prompted username and password.
So, it's exactly the same thing but from the other side, if it's possible to provide information to the authentication prompt on request, how could I require this authentication on the request instead?
So everytime somebody tries to make a request to my application on example:
http://myapplication/clients/verify_credentials
it should ask for a username and password with that server prompt
so to retrive information on curl for example it would be like this
curl -u user:password http://myapplication/clients/verify_credentials
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
那么,要要求基本身份验证,您需要返回 401 状态代码。但这样做将导致当前身份验证模块执行其默认的未经授权处理程序(对于表单身份验证,这意味着重定向到登录页面)。
我编写了一个
ActionFilterAttribte
来看看当web.config
中没有安装身份验证模块时是否可以获得您想要的行为。控制器操作:
该操作成功打印我输入的用户名和密码。但我真的怀疑这是最好的方法。除了以这种方式询问用户名和密码之外,您别无选择吗?
Well, to require basic authentication you need to return 401 status code. But doing that will cause the current authentication module to execute its default unauthorized handler (for forms authentication, this means redirecting to login page).
I wrote an
ActionFilterAttribte
to see if I can get the behaviour you want when there's no authentication module installed inweb.config
.And the controller action :
That action successfully prints the username and password I enter. But I really doubt that's the best way to do this. Do you have no choice except asking for username and password this way?
这是对我有用的方法。这是一个小工作,但它将使 IIS 和 MVC3 的行为更像所有其他基本 Http 身份验证系统,例如 Apache...
第 1 步。
步骤2。
步骤 3.
步骤 3a。 [编辑]
第 4 步。
...差不多就是这样了。
Here's the way that has worked for me. It's a little foot work but it will make IIS and MVC3 behave a lot more like all the other Basic Http authentication systems, like Apache...
Step 1.
Step 2.
Step 3.
Step 3a. [edit]
Step 4.
...and that's pretty much it.
我修改了 çağdaş 答案,将整个逻辑放入我的自定义 ActionFilter 属性中。
它可用于将整个控制器置于基本身份验证下:
或特定的 ActionResult:
注意:上述实现要求开发人员手动插入用户名和密码作为 ActionFilter 所需的参数,但可以轻松扩展为通过删除自定义构造函数并相应地修改 OnActionExecuting 方法 IF 块,使其支持任何授权机制(MembershipProvider、ASP.NET Identity、外部 DBMS 或文件上的自定义用户库等)。
有关其他信息,您还可以 阅读我在博客上写的这篇文章。
I modified the çağdaş answer to put the whole logic inside my custom ActionFilter attribute.
It can be used to put under Basic Authentication a whole controller:
or a specific ActionResult:
NOTE: The above implementation requires the developer to manually insert the username and password as ActionFilter required parameters but can be easily extended to make it support any authorization mechanism (MembershipProvider, ASP.NET Identity, custom userbase on an external DBMS or file, etc.) by removing the custom constructor and modifying the OnActionExecuting method IF block accordingly.
For additional info, you can also read this post I wrote on my blog.
根据我所读到的内容,您确实想要创建一个服务而不是一个 Web 应用程序。我在这里猜测,但我认为您选择了 ASP.NET MVC 来利用路由并按照您想要的方式构建 URL?如果我错了请纠正我。
在我看来,解决您遇到的问题的最佳方法是使用 WCF 构建 RESTful Web 服务(如果您要返回数据)。如果您想走这条路,这篇文章应该可以帮助您入门。
否则,您将需要进一步深入堆栈来处理请求并对其进行身份验证。如果是这种情况,我可以帮助提供更多信息和代码。
You really want to create a service and not a web application, based on what I have read. I am guessing here, but I think you picked ASP.NET MVC to take advantage of the routing and building the URL's the way you want? Correct me if I am wrong.
In my opinion the best way to solve the problem you are having is to build RESTful web services with WCF if you are returning data. This article should help you get started if you want to go this route.
Otherwise, you will need to go further up the stack for handling the request and authenticating it. If this is the case, I can help with providing more info and code.