2014-10-29 47 views
1

我有一个Xamarin移动应用程序,它使用Azure移动服务SDK来针对社交提供者验证用户。我得到令牌并将其附加到http请求中,作为持票人令牌,将我的Web API ASP.NET应用程序(我正在部署为Azure Cloud Service)附加到该应用程序中。Azure移动服务令牌验证在Web API和MVC

我需要验证持票人令牌并获取ClaimsIdentity以在我的ApiController中使用。我是否需要为此使用Mobile Service .NET后端nuget包?我怎么能够?

编辑:

  1. 添加创建一个Web API控制器

  2. 安装的移动服务.NET后端NuGet包

  3. 设置的AppSettings值的键MS_MobileServiceName一个空的ASP.NET应用程序, MS_MasterKey, Azure管理控制台中的值MS_ApplicationKey

  4. 设置[AuthorizeLevel(AuthorizationLevel.User)]在我ApiController我HTTPGET操作

  5. 演员表的,以ServiceUser

  6. 创建与谷歌验证承载令牌通天青 移动服务SDK

  7. HTTP请求
  8. 用户为空!

回答

0

使用属性,如

[AuthorizeLevel(AuthorizationLevel.Anonymous)] 

[AuthorizeLevel(AuthorizationLevel.User)] 

在您的API中的方法取决于你想要的角色进行验证。

然后使用

var currentUser = User as ServiceUser; 

获得当前用户的方法,如果AuthorizationLevel不是匿名。

并根据需要设置您的AppSettings。如果您正在测试本地主机上的MobileService,请更改MasterKey和ApplicationKey。

<appSettings> 
    <!-- Use these settings for local development. After publishing to 
    Mobile Services, these settings will be overridden by the values specified 
    in the portal. --> 
    <add key="MS_MobileServiceName" value="[NAME HERE]" /> 
    <add key="MS_MasterKey" value="[INSERT HERE]" /> 
    <add key="MS_ApplicationKey" value="[INSERT HERE]" /> 
    <add key="MS_MicrosoftClientID" value="Overridden by portal settings" /> 
    <add key="MS_MicrosoftClientSecret" value="Overridden by portal settings" /> 
    <add key="MS_FacebookAppID" value="Overridden by portal settings" /> 
    <add key="MS_FacebookAppSecret" value="Overridden by portal settings" /> 
    <add key="MS_GoogleClientID" value="Overridden by portal settings" /> 
    <add key="MS_GoogleClientSecret" value="Overridden by portal settings" /> 
    <add key="MS_TwitterConsumerKey" value="Overridden by portal settings" /> 
    <add key="MS_TwitterConsumerSecret" value="Overridden by portal settings" /> 
    <add key="MS_AadClientID" value="Overridden by portal settings" /> 
    <add key="MS_AadTenants" value="Overridden by portal settings" /> 
    <!-- When using this setting, be sure to also set the Notification Hubs connection 
    string named "MS_NotificationHubConnectionString". --> 
    <add key="MS_NotificationHubName" value="Overridden by portal settings" /> 
    <add key="Microsoft.ServiceBus.ConnectionString" value="Endpoint=sb://[your namespace].servicebus.windows.net;SharedSecretIssuer=owner;SharedSecretValue=[your secret]" /> 
    </appSettings> 
+0

当然我必须设置某种身份验证提供的在我的WebApiConfig,并把我的主密钥在什么地方,对吧? – 2014-10-29 03:35:10

+0

那么你需要NuGet的Azure移动服务.NET后端包。 Master和ApplicationKey存储在Web.Config中。但就是这样。 – 2014-10-29 03:41:59

+0

你能否提供一些代码示例来做到这一点?我没有找到如何注册WebApiConfig。 – 2014-10-29 12:54:18

相关问题