简体   繁体   English

Azure移动服务和Web API身份验证

[英]Azure Mobile Services and Web API authentication

I'm developing a Web API and was looking to use Azure Mobile Services to authenticate users before allowing calls made to the Web API. 我正在开发Web API,并希望在允许对Web API进行调用之前使用Azure Mobile Services对用户进行身份验证。

So the user would navigate to a website, choose to log in using their Google/Facebook/etc account and the user would be authenticated using the Mobile Services JavaScript client. 因此,用户将导航到网站,选择使用他们的Google / Facebook / etc帐户登录,并且将使用移动服务JavaScript客户端对用户进行身份验证。 From what I understand Mobile Services will then return a authentication token in the form of a JSON Web Token. 据我所知,移动服务将以JSON Web令牌的形式返回身份验证令牌。

What I would like to do is when website calls the Web API it would pass along the authentication token, the Web API would check that it's a valid token issued by Mobile Services and if all is good, then allow the call to be executed. 我想做的是当网站调用它将传递给身份验证令牌的Web API时,Web API将检查它是否是移动服务发出的有效令牌,如果一切正常,则允许执行调用。

So my question is...is this possible? 所以我的问题是......这可能吗? If so, could the JSON Web Token Handler for .NET be used to perform the validation on the Web API side? 如果是这样,是否可以使用JSON Web Token Handler for .NET在Web API端执行验证?

Yes, that is possible. 是的,这是可能的。

If you perform a login using the MobileServiceClient, you will get a token that you can pass along with every request to a Web Api endpoint. 如果使用MobileServiceClient执行登录,您将获得一个令牌,您可以将其传递给Web Api端点的每个请求。

var client = new WindowsAzure.MobileServiceClient('https://yourservice.azure-mobile.net', 'your-client-key');
client.login('facebook').then(success);

function success(result) {
    alert('login ok');
}

So when making a request, set the value of header 'X-ZUMO-AUTH' to the current users token you find in client.currentUser.mobileServiceAuthenticationToken after a successful login. 因此,在发出请求时,将标题'X-ZUMO-AUTH'的值设置为在成功登录后在client.currentUser.mobileServiceAuthenticationToken找到的当前用户标记。

On the server side, add the attribute [AuthorizeLevel(AuthorizationLevel.User)] to Web Api methods that require the user to be authenticated. 在服务器端,将属性[AuthorizeLevel(AuthorizationLevel.User)]到需要用户进行身份验证的Web Api方法。 Thats all. 就这样。

But make sure, that identity is configured properly on WAMS, and also at the provider side you want to integrate (client id's, client secrets, callback urls, etc.). 但请确保,在WAMS上正确配置了该身份,并且还要在提供者端配置您要集成的身份(客户端ID,客户端机密,回调URL等)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM