2014-07-17 7 views
5

मैं एक कोणीय जेएस सिंगल पेज एप्लिकेशन (एसपीए) से ओविन पर निर्मित एक वेब एपीआई से प्रमाणीकरण स्थापित करने की कोशिश कर रहा हूं। यहाँ मैं क्या है ...मैं कोणीय एसपीए से वेब एपीआई 2 के खिलाफ उपयोगकर्ता प्रमाणीकरण के लिए जेडब्ल्यूटी का उपयोग कैसे करूं?

यह लॉगिन समारोह (एक AuthenticationController पर पोस्ट कार्रवाई एपीआई में)

public HttpResponseMessage login(Credentials creds) 
    { 
     if (creds.Password == "password" && creds.Username.ToLower() == "username") 
     { 
      var user = new User { UserId = 101, Name = "John Doe", Role = "admin" }; 

      var tokenDescriptor = new SecurityTokenDescriptor() 
      { 
       Subject = new ClaimsIdentity(new[] 
       { 
        new Claim(ClaimTypes.Name, user.Name), 
        new Claim(ClaimTypes.Role, user.Role) 
       }), 

       AppliesToAddress = "http://localhost:/8080", 
       TokenIssuerName = "myApi", 

       SigningCredentials = new SigningCredentials(new InMemorySymmetricSecurityKey(TestApp.Api.Startup.GetBytes("ThisIsTopSecret")), 
        "http://www.w3.org/2001/04/xmldsig-more#hmac-sha256", 
        "http://www.w3.org/2001/04/xmlenc#sha256") 
      }; 

      var tokenHandler = new JwtSecurityTokenHandler(); 
      var token = tokenHandler.CreateToken(tokenDescriptor); 
      var tokenString = tokenHandler.WriteToken(token); 

      return Request.CreateResponse<LoginResponse>(HttpStatusCode.Accepted, new LoginResponse { User = user, AuthToken = tokenString }); 
     } 

     return Request.CreateResponse(HttpStatusCode.Unauthorized); 
    } 

है यह मेरा Owin स्टार्टअप config है

public void Configuration(IAppBuilder app) 
    { 
     app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); 

     HttpConfiguration config = new HttpConfiguration(); 
     config.Routes.MapHttpRoute(
      name: "Default", 
      routeTemplate: "{controller}" 
     ); 

     var jsonSettings = config.Formatters.JsonFormatter.SerializerSettings; 
     jsonSettings.Formatting = Formatting.Indented; 
     jsonSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); 

     app.UseWebApi(config); 

     app.UseJwtBearerAuthentication(
     new JwtBearerAuthenticationOptions 
     { 
      AuthenticationMode = AuthenticationMode.Active, 
      AllowedAudiences = new[] { "http://localhost:8080" }, 
      IssuerSecurityTokenProviders = new IIssuerSecurityTokenProvider[] 
      { 
       new SymmetricKeyIssuerSecurityTokenProvider("myApp", TestApp.Api.Startup.GetBytes("ThisIsTopSecret")) 
      } 
     }); 
    } 

यह कोणीय है कोड जो मैं एपीआई में नियंत्रक को कॉल करने के लिए उपयोग कर रहा हूं जिसमें प्राधिकृत विशेषता है।

$http({ method: 'GET', url: 'http://localhost:5000/Admin', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + Session.token }}) 
      .then(function (res) { 
       return res.data; 
      }); 

जब मैं लॉगिन करता हूं तो मुझे स्ट्रिंग के रूप में लौटा टोकन मिलता है। मैं इसे अपने क्लाइंट साइड सत्र में स्टोर करता हूं। इसके बाद मैं इसे बाद में अनुरोधों के लिए अपने हेडर में रखता हूं।

जब मैं एपीआई में "अधिकृत" कार्रवाई को कॉल करने का प्रयास करता हूं तो मुझे 401 प्रतिक्रिया मिलती है, भले ही मेरा टोकन अनुरोध के शीर्षलेख के माध्यम से पारित हो गया हो।

मैं जेडब्ल्यूटी के लिए नया हूं इसलिए मैं अपने दृष्टिकोण पर पूरी तरह से आधार से बाहर हो सकता हूं। कोई भी सलाह बहुत उपयोगी होगी।

उत्तर

4

मेरा मानना ​​है कि यह संचालन की बात है। ऐप के ऊपर app.UseJwt कथन को ले जाएं .UseWebApi लाइन।

app.UseJwtBearerAuthentication(
    new JwtBearerAuthenticationOptions 
    { 
     AuthenticationMode = AuthenticationMode.Active, 
     AllowedAudiences = new[] { "http://localhost:8080" }, 
     IssuerSecurityTokenProviders = new IIssuerSecurityTokenProvider[] 
     { 
      new SymmetricKeyIssuerSecurityTokenProvider("myApp", TestApp.Api.Startup.GetBytes("ThisIsTopSecret")) 
     } 
    }); 

app.UseWebApi(config); 
+1

आपको मुझसे मजाक करना होगा ... यह मेरे लिए काम करता है! –

+0

@ सैम, https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware – jsign

संबंधित मुद्दे