2017-05-09 14 views
1

Web APIと通信するWindowsアプリケーションを作成しています。電話をかける方法は次のとおりです。サーバー上のWeb API認証

HttpClient client = null; 
     HttpClientHandler handler = new HttpClientHandler() { PreAuthenticate = true, Credentials = CredentialCache.DefaultCredentials }; 
     client = new HttpClient(); 
     client.BaseAddress = new Uri(apiBaseAddress); 
     var byteArray = Encoding.ASCII.GetBytes(Environment.UserName); 
     client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 
     client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray)); 
     HttpResponseMessage response = client.GetAsync("api/Tickets/AuthenticateUser").Result; 

現在ログオンしている資格情報を渡しています。私はdbに接続し、ユーザー名が存在するかどうかをチェックするフィルターを作成しました。コード:

public class BasicAuthenticationWindowsAppAttribute : System.Web.Http.Filters..AuthorizationFilterAttribute 
{ 
    public override void OnAuthorization(System.Web.Http.Controllers.HttpActionContext actionContext) 
    { 
     if (actionContext.Request.Headers.Authorization == null) 
     { 
      actionContext.Response = new System.Net.Http.HttpResponseMessage(System.Net.HttpStatusCode.Unauthorized); 
     } 
     else 
     { 
      string authToken = actionContext.Request.Headers.Authorization.Parameter; 
      string Handle = Encoding.UTF8.GetString(Convert.FromBase64String(authToken)); 
      GenericIdentity gi = new GenericIdentity(Handle); 
      Thread.CurrentPrincipal = new GenericPrincipal(gi, null); 
      HttpContext.Current.User = Thread.CurrentPrincipal; 
      Amo_MasterDataEntities amoMasterDataContext = new Amo_MasterDataEntities(); 
      var query = from a in amoMasterDataContext.allassociatemasters 
         where a.Handle == Handle 
         select a; 
      //If Handle is present in AMOMasterData.AllAssociatemaster table 

      if (query.Count() > 0) 
      { 
       //TicketsController tc = new TicketsController(); 
       string assocId = "", fName ="", lName = ""; 
       bool authenticated = false; 
       foreach (var item in query) 
       { 
        assocId = item.AssociateID; 
        fName = item.FirstName; 
        lName = item.LastName; 
        authenticated = true; 
       } 
       AuthInfo info = new AuthInfo(); 
       info.AssociateId = assocId; 
       info.FirstName = fName; 
       info.LastName = lName; 
       info.IsAuthenticated = authenticated; 
       actionContext.Request.Properties.Add(new KeyValuePair<string, object>("AuthInfo", info)); 
       base.OnAuthorization(actionContext); 

      } 
      //else return error 
      else 
      actionContext.Response = new System.Net.Http.HttpResponseMessage(System.Net.HttpStatusCode.Unauthorized); 

     } 
    } 
} 

ローカルシステムでWebサービスを実行すると、動作します。しかし、サーバーにWebサービスを展開すると、401 Unauthorized Messageが届きます。

私はIISで基本とWindows認証の両方を有効にしているとのWeb.configは<authentication mode="Windows" />

編集含まれています。私は、私が展開しているサーバーからWeb APIのメソッドにアクセスすることができる午前 を。 しかし、別のマシンのWindowsクライアントからWeb APIを呼び出すと、401エラーが発生します。

CORSを使用しますか?はいの場合はどうすればいいのか教えてください。

誰にでも私のために解決策を教えてくれますか?

+0

''を取り出してもうまくいきますか? –

+0

こんにちは。私は401を取得しているを削除しようとしました。 –

+0

あなたは 'OnAuthorization'メソッドであらゆる種類のログを投げ込もうとしましたか? –

答えて

0

私は私が問題だったのHttpClientのコンストラクタにハンドラを渡していませんでした

..理由を発見しました。したがって、上記のコードに置き換えます:

client = new HttpClient(); 

で:

client = new HttpClient(handler); 

このような愚かな間違い。ご迷惑おかけして申し訳ありません。