2011-12-15 49 views
4

我有一個具有單個模式的ODATA服務。這些指向開發數據庫,​​並通過WCF數據服務提供服務,然後由運行Excel/Powerpivot的客戶端使用WCF數據服務來獲取他們自己的報告等數據。在運行時爲使用基本身份驗證的OData/WCF數據服務更改連接字符串

的服務是通過幾乎相同的基本身份驗證確保在運行時解釋這裏:http://msdn.microsoft.com/en-us/data/gg192997

現在怎麼這需要在實際環境中的工作是坐在服務器上,並連接到基於用戶名不同的數據庫/提供密碼。用戶將輸入'username @ clientID'和'password'。 'username @ clientID'然後被分割()並且針對SQL數據庫檢查用戶名/密碼。但要檢查的數據庫服務器URL將由ClientID決定。

另外,一旦它被授權,WCF數據服務需要從數據庫返回對應於ClientID的數據。

我試過的方法是修改web.config文件中的連接字符串,但這不起作用,因爲它說該文件是隻讀的。我甚至不確定這是否會起作用。我需要做的是讓EDMX/WCF數據服務從正確的數據庫返回數據。這裏就是我試圖做的:

private static bool TryAuthenticate(string user, string password, out IPrincipal principal) 
    { 

     Configuration myWebConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~"); 
     myWebConfig.AppSettings.Settings["test"].Value = "Hello"; 
     myWebConfig.Save(); 

     string newConnStr = myWebConfig.ConnectionStrings.ConnectionStrings["IntelCorpEntities"].ToString(); 
     newConnStr.ToString().Replace("SERGEIX01", "SERVERX01"); 
     myWebConfig.ConnectionStrings.ConnectionStrings["IntelCorpEntities"].ConnectionString = newConnStr;    
     myWebConfig.Save(); 

     if (user.ToLower().Equals("admin") && password.Equals("password")) 
     { 
      principal = new GenericPrincipal(new GenericIdentity(user), new string[] { "Users" }); 
      return true; 
     } 
     else 
     { 
      principal = null; 
      return false; 
     } 
    } 

回答

5

在你的DataService派生類中重寫的createDataSource方法,並在其中找出正確的連接字符串,創建一個連接字符串的EF對象上下文的新實例,並將其返回。 WCF DS服務將不會使用EF對象上下文的默認構造函數,那麼完全取決於您使用正確的連接字符串構造實例。

+0

謝謝!這工作。對不起,我可以upvote。它說我需要15個代表。 – Rash 2011-12-15 15:20:37

1

在你svc.cs文件中添加以下內容:

protected override NorthWindEntity CreateDataSource() 
{ 
    System.Data.EntityClient.EntityConnection connection = new System.Data.EntityClient.EntityConnection(); 
    connection.ConnectionString = ""; 
    NorthWindEntity ctx = new NorthWindEntity(connection); 
    return ctx; 
} 
相關問題