很多朋友詢問我如何設置ADO.NET Data Service的權限.其實設置ADO.NET Data Service的權限與正常設置ASPNET網站的資源訪問權限方式一樣.
下面我就已ADO.NET Data Service文中的例子為原型,說明一下該如何設置資源訪問權限
先規劃一下ASPNET網站的資源結構
在Web.config中按如上設計進行權限配置
<authentication mode="Forms"> <forms name="poweridtag" loginUrl="publicweb/login.aspx" /> </authentication> <authorization> <deny users="?"> </deny> </authorization> <location path="ClientBin"> <system.web> <authorization> <allow users="*"> </allow> </authorization> </system.web> </location> <location path="publicweb"> <system.web> <authorization> <allow users="*"> </allow> </authorization> </system.web> </location>
在publicweb中添加一loginService.svc服務,用於提供登錄
[ServiceContract(Namespace = "")] [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class loginService { [OperationContract] public string login(string userID ,string password) { //判斷 userID 與 password System.Web.Security.FormsAuthentication.SetAuthCookie("dbadmin", false); return "ok"; } }
在SilverlightApplication中添加對loginService.svc服務的引用,並用如下方式完成登錄
private void login_Click(object sender, RoutedEventArgs e) { loginServer.loginServiceClient lsr = new SilverlightApplication.loginServer.loginServiceClient(); lsr.loginCompleted += new EventHandler<SilverlightApplication.loginServer.loginCompletedEventArgs>(lsr_loginCompleted); lsr.loginAsync(this.userID.Text,this.password.Text); } void lsr_loginCompleted(object sender, SilverlightApplication.loginServer.loginCompletedEventArgs e) { if (e.Result == "ok") { MessageBox.Show("登錄成功"); } }
看一下效果