使用属性,如
[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>
当然我必须设置某种身份验证提供的在我的WebApiConfig,并把我的主密钥在什么地方,对吧? – 2014-10-29 03:35:10
那么你需要NuGet的Azure移动服务.NET后端包。 Master和ApplicationKey存储在Web.Config中。但就是这样。 – 2014-10-29 03:41:59
你能否提供一些代码示例来做到这一点?我没有找到如何注册WebApiConfig。 – 2014-10-29 12:54:18