简体   繁体   English

ASP.NET MVC4和Web API身份验证+授权(Windows 8和Web)

[英]ASP.NET MVC4 and Web API Authentication + Authorization (Windows 8 and Web)

I created an ASP.NET MVC 4 Internet Web Application using Simple Membership db for storing users, roles and profiles. 我使用Simple Membership db创建了一个ASP.NET MVC 4 Internet Web应用程序,用于存储用户,角色和配置文件。

In my app I created a Web API controller that will respond to the http:// 127.0.0.1/api/users and this call will return a list of all users stored in the db as JSON . 在我的应用程序中,我创建了一个Web API控制器,它将响应http:// 127.0.0.1/api/users ,此调用将返回存储在数据库中的所有用户的列表作为JSON

The UsersController is decorated with [Authorize] attribute therefore any call to the http:// 127.0.0.1/api/users have to be authenticated. UsersController使用[Authorize]属性进行修饰,因此必须对http:// 127.0.0.1/api/users任何调用进行身份验证。

On the main page I have a button that once pressed a jQuery ajax get request is performed and will show the list of users returned by the Web API (json). 在主页面上,我有一个按钮,一旦按下jQuery ajax get请求就会执行,并将显示Web API(json)返回的用户列表。

If I am logged in and I press the button, everything works fine ... the list of users is retrieved and shown. 如果我登录并按下按钮,一切正常......检索并显示用户列表。

If I am not logged in, the message show is 401 - Unauthorized (custom message in jquery call statusCode) so it works as it should because of the [Authorize] attribute. 如果我没有登录,则消息显示为401 - Unauthorized (jquery调用statusCode中的自定义消息),因此它可以正常工作,因为[Authorize]属性。

My question: How can I authenticate when a Web API makes a request to it, to get the list of users and show them from a Windows 8 app or Windows Phone 8 or Windows Forms for example? 我的问题:我如何验证Web API何时向其发出请求,获取用户列表并从Windows 8应用程序或Windows Phone 8或Windows窗体中显示它们? In other words, from a client other than the web browser? 换句话说,来自Web浏览器以外的客户端?

That depends on how you're coding your app, but basically, you'll use http protocol for this. 这取决于你如何编写你的应用程序,但基本上,你将使用http协议。 Take a look at ASP.NET MVC - HTTP Authentication Prompt and http://www.piotrwalat.net/basic-http-authentication-in-asp-net-web-api-using-membership-provider/ 看看ASP.NET MVC - HTTP身份验证提示http://www.piotrwalat.net/basic-http-authentication-in-asp-net-web-api-using-membership-provider/

You should make another api, that checks the authentication from the Windows 8 app or Windows. 您应该创建另一个api,它从Windows 8应用程序或Windows检查身份验证。

If its pass successfully from the authentication, then only you have to show user list to the client. 如果成功通过身份验证,则只需要向客户端显示用户列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 asp.net mvc4中的web.config中的Windows身份验证 - Windows Authentication in web.config in asp.net mvc4 授权和身份验证ASP.Net Web API - Authorization and Authentication ASP.Net web api ASP.NET MVC4 Web ZDB974238714CA8DE634A7CE1D08 中的 URL 路由 - URL route in ASP.NET MVC4 Web API ASP.NET MVC4 Web API接口作为数据协定 - ASP.NET MVC4 Web API Interfaces as Data Contract 如何在与 Web API 通信的 ASP.NET Core MVC 应用程序中使用自定义身份验证/授权? - How to use custom authentication/authorization in an ASP.NET Core MVC app that communicates with a Web API? 具有角色的ASP.NET Web API中的身份验证和授权 - Authentication and Authorization in ASP.NET Web API with roles 处理身份验证/授权:ASP.NET Core Web 应用程序 => ASP.NET Core Web API => SQL - Handling authentication/authorization: ASP.NET Core Web Application => ASP.NET Core Web API => SQL Web API的ASP.Net MVC自定义授权 - ASP.Net MVC Custom Authorization for Web API 来自另一个Web API应用程序的身份验证和授权ASP.NET Web API - Authentication and authorization ASP.NET Web API from another Web API Application 如何在 ASP.Net MVC4 Web API 项目中使用 Microsoft OCR 库 (Microsoft.Windows.Ocr)? - How to use Microsoft OCR Library ( Microsoft.Windows.Ocr ) in an ASP.Net MVC4 Web API Project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM