0

我正在爲我的網站使用asp.net樣板。我有aspnetboilerplate/module-zero(OWIN)的標準認證。從Windows Phone 8.1的ASP.NET鍋板驗證 - 不記名令牌

但現在我需要爲我的Windows Phone應用程序(wp8.1) 我試圖配置我與承載授權的應用程序,但我失敗了athentication .. 如何configurate asp.net樣板申請我的Windows Phone應用程序權威性?

在Windows Phone的應用程序我發帖子到我的網頁API這樣的:

public static async Task<TokenResponseModel> GetBearerToken(string siteUrl, string Username, string Password) 
     { 
      HttpClient client = new HttpClient(); 
      client.BaseAddress = new Uri(siteUrl); 
      client.DefaultRequestHeaders.Accept.Clear(); 

      HttpContent requestContent = new StringContent("grant_type=password&username=" + Username + "&password=" + Password, Encoding.UTF8, "application/x-www-form-urlencoded"); 

      HttpResponseMessage responseMessage = await client.PostAsync("Token", requestContent); 

      if (responseMessage.IsSuccessStatusCode) 
      { 
       string jsonMessage; 
       using (Stream responseStream = await responseMessage.Content.ReadAsStreamAsync()) 
       { 
        jsonMessage = new StreamReader(responseStream).ReadToEnd(); 
       } 

       TokenResponseModel tokenResponse = (TokenResponseModel)JsonConvert.DeserializeObject(jsonMessage, typeof(TokenResponseModel)); 

       return tokenResponse; 
      } 
      else 
      { 
       return null; 
      } 
     } 

但我應該在的WebAPI辦? [AbpAuthorize]在下一步如何使用持票人時,如何授權和下一個響應持有人以及如何授權?

+0

可以是它會幫助你 [ASP .NET身份和移動客戶端] :http://stackoverflow.com/questions/22126305/asp-net-identity-and-mobile-clients –

回答

0

這現在記錄,並在模塊中實現零模板

代碼: 在模塊的WebAPI:

Configuration.Modules.AbpWebApi().HttpConfiguration.Filters.Add(new HostAuthenticationFilter("Bearer")); 

在控制器的WebAPI:

[HttpPost] 
    public async Task<AjaxResponse> Authenticate(LoginModel loginModel) 
    { 
     CheckModelState(); 

     var loginResult = await GetLoginResultAsync(
      loginModel.UsernameOrEmailAddress, 
      loginModel.Password, 
      loginModel.TenancyName 
      ); 

     var ticket = new AuthenticationTicket(loginResult.Identity, new AuthenticationProperties()); 

     var currentUtc = new SystemClock().UtcNow; 
     ticket.Properties.IssuedUtc = currentUtc; 
     ticket.Properties.ExpiresUtc = currentUtc.Add(TimeSpan.FromMinutes(30)); 

     return new AjaxResponse(OAuthBearerOptions.AccessTokenFormat.Protect(ticket)); 
    } 

文檔:http://aspnetboilerplate.com/Pages/Documents/Zero/Startup-Template#token-based-authentication