1

我在與網頁API 2.獲得2

我使用vs2015並開發我的asp.net的MVC單頁模板項目使用基因敲除和薩米麻煩問題訪問令牌Asp.NET網絡API通過owin中間件獲得/授權身份。

當我通過默認的單個頁面app.js請求訪問令牌時,這很奏效,但是如果我嘗試通過郵遞員獲得令牌(grant_type=password&[email protected]&password=1234),則返回invalid_cliend錯誤。

{ 
    "error": "invalid_client" 
} 

提供者:

public class ApplicationOAuthProvider : OAuthAuthorizationServerProvider 
    { 
     private readonly string _publicClientId; 

     public ApplicationOAuthProvider(string publicClientId) 
     { 
      if (publicClientId == null) 
      { 
       throw new ArgumentNullException("publicClientId"); 
      } 

      _publicClientId = publicClientId; 
     } 

     public override Task ValidateClientRedirectUri(OAuthValidateClientRedirectUriContext context) 
     { 
      if (context.ClientId == _publicClientId) 
      { 
       Uri expectedRootUri = new Uri(context.Request.Uri, "/"); 

       if (expectedRootUri.AbsoluteUri == context.RedirectUri) 
       { 
        context.Validated(); 
       } 
       else if (context.ClientId == "web") 
       { 
        var expectedUri = new Uri(context.Request.Uri, "/"); 
        context.Validated(expectedUri.AbsoluteUri); 
       } 
      } 

      return Task.FromResult<object>(null); 
     } 

    } 

Startup.Auth:

static Startup() 
     { 
      PublicClientId = "web"; 

      OAuthOptions = new OAuthAuthorizationServerOptions 
      { 
       TokenEndpointPath = new PathString("/Token"), 
       AuthorizeEndpointPath = new PathString("/Account/Authorize"), 
       Provider = new ApplicationOAuthProvider(PublicClientId), 
       //Provider = new AuthorizationServerProvider(), 
       AccessTokenExpireTimeSpan = TimeSpan.FromDays(14), 
       AllowInsecureHttp = true 
      }; 
     } 

     public static OAuthAuthorizationServerOptions OAuthOptions { get; private set; } 

     public static string PublicClientId { get; private set; } 

我需要你的幫助。

+0

確保你是在請求體發送'grant_type =密碼和用戶名=管理員@ mail.com和密碼= 1234'。你如何撰寫郵遞員的要求?可能的話,你可以使用'Fiddler'來截取你的'app.js'中的請求(你認爲它工作正常),看看它是如何被髮送然後進行比較的。 –

+0

轉到「Body」選項卡 - >選中「x-www-form-urlencoded」並傳遞grant_type,用戶名和密碼作爲鍵/值對。 – tsubasaetkisi

回答

0

我認爲當您要使用類型密碼授權(grant_type = password)時,您必須重寫ValidateClientAuthentication而不是ValidateClientRedirectUri。

public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context) 
{ 
    //here simply call context.Validated() or add your client id validation logic 

} 
+0

準確!謝謝你的回答。 – tsubasaetkisi

0

爲他人解決辦法:

public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context) 
{ 
    context.Validated(); 
} 

public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) 
{ 
    // set CORS 
    context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" }); 

    //validate to get access_token 
    if (context.UserName == "[email protected]" && context.Password == "1234") 
    { 
     var identity = new ClaimsIdentity(context.Options.AuthenticationType); 


     identity.AddClaim(new Claim("sub", context.UserName)); 
     identity.AddClaim(new Claim("role", "user")); 

     context.Validated(identity); 
    } 
    else 
    { 
     context.SetError("invalid_grant", "Invalid username or password."); 
    } 
}