[英]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.