使用屬性,如
[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