在 MVC 2 中验证 REST 请求

发布于 2024-10-01 07:28:46 字数 204 浏览 0 评论 0原文

嘿,SO,在过去的几个小时里,我试图了解可以通过 asp.net MVC 提供的 RESTful 服务。我在互联网上找到的所有教程和指南似乎都没有涵盖身份验证。

目前,我们在现有的 MVC 应用程序中使用基于表单的身份验证。据我了解,我们需要添加基本 HTTP 身份验证才能处理 REST 请求和连接到用户上下文的用户权限。有没有办法在一个应用程序中“混合”这两种身份验证模式?

Hey SO, in the past few hours I was trying to get my head around RESTful services that can be served via asp.net MVC. Authentication is still something that doesn't seem to be covered in all those tutorials and guides i was finding in the interwebs.

Currently we are using Forms Based Authentication in our existing MVC Application. As far as I understand we need to add Basic HTTP Authentication to be able to handle REST requests and user permissions connected to the user context. Is there any way to "mix" these two Authentication Modes in one Application?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柒夜笙歌凉 2024-10-08 07:28:46

我不确定是否有内置的东西,但你可以自己编写。类似于:

var authHeader = Request.ServerVariables["HTTP_AUTHORIZATION"];
if (authHeader.StartsWith("Basic ", StringComparison.InvariantCultureIgnoreCase))
{
    var authParams = Encoding.Default.GetString(Convert.FromBase64String(authHeader.Substring("Basic ".Length)));
    var arr = authParams.Split(':');
    var username = arr[0];
    var password = arr[1];
}   

如果您在 MVC 中编写自己的 REST 框架,则可以有一个基本的 Controller 类,并有一个与此类似的方法,该方法在每个操作之前运行以对调用者进行身份验证。

I'm not sure if there's anything built in, but you can write your own. Something like:

var authHeader = Request.ServerVariables["HTTP_AUTHORIZATION"];
if (authHeader.StartsWith("Basic ", StringComparison.InvariantCultureIgnoreCase))
{
    var authParams = Encoding.Default.GetString(Convert.FromBase64String(authHeader.Substring("Basic ".Length)));
    var arr = authParams.Split(':');
    var username = arr[0];
    var password = arr[1];
}   

If you're writing your own REST framework in MVC, you could have a base Controller class, and have a method similar to this that runs before each action to authenticate the caller.

猫烠⑼条掵仅有一顆心 2024-10-08 07:28:46

戴夫,

我明白你的意思。会员框架广泛使用 cookie 进行身份验证。您将凭据传递给服务器,服务器根据用户数据库验证它们并向您颁发身份验证 cookie。下次 urs 的每次调用都包含此身份验证 cookie,服务器使用该身份验证 cookie 来对用户进行身份验证和授权。现在,当您使用浏览器时,整个工作流程可以无缝运行。

现在,在您的场景中,您可以在控制器中创建一个用于验证凭据的操作。您可以在发布/获取数据中将凭据传递给此操作。您必须将身份验证 cookie 保存在代码中,并在每次调用服务器时包含该 cookie。您可以重写 HttpWebRequest 类来执行这些步骤,并且可以在代码中使用相同的类。

如果这是很大的开销,并且您正在寻找类似 Web 服务之类的功能,我会建议您研究 WCF 服务/Ado.NET 数据服务。这些与会员框架更加无缝地集成,并且可能更适合您的结果。

我希望这有帮助,谢谢。

Dave,

I understand your point. Membership framework uses cookie extensively for authentication. You pass your credentials to server, server validates them against user database and issue you an authentication cookie. Next time every call of urs contains this authentication cookie which server uses to authenticate and authorise the user. Now whole this workflow works seamlessly when you use browsers.

Now in your scenario, you can create an Action in a controller which validates credentials. You can pass credentials to this Action in either post/get data. You will have to save the authentication cookie in your code and include that each time when making a call to the server . You can override HttpWebRequest class to perform these steps and you can use same class in your code.

In case this is much of an overhead and you are looking for something like Web-Services sort of functionality, I will advice you to look into WCF Services / Ado.NET Data Services. These integrate with Membership framework more seamlessly and may be better suited to your results.

I hope this helps, thanks.

与往事干杯 2024-10-08 07:28:46

您可以轻松地将 ASP.NET 成员资格框架与 ASP.NET MVC RESTful 服务结合使用。请参阅以下链接了解其使用 MVC RESTful 服务的实现。

http://msdn.microsoft.com/en-us/magazine/dd943053.aspx

如果您不知道会员资格框架,请使用以下链接

http:// /msdn.microsoft.com/en-us/library/yh26yfzy.aspx

希望有帮助,谢谢

You can easily use ASP.NET membership framework with ASP.NET MVC RESTful services. See the following link for its implementation with MVC RESTful services.

http://msdn.microsoft.com/en-us/magazine/dd943053.aspx

In case you are not aware of membership framework use following link

http://msdn.microsoft.com/en-us/library/yh26yfzy.aspx

I hope it helps, thanks

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文