2016-09-28 6 views
9

मैं IdentityServer4 एक मैं 3 diferents proyects बनाने हूँ लागू करने हूँ:साथ और ClientCredentials साथ जावास्क्रिप्ट ग्राहक IdentityServer4 का उपयोग कैसे करें ASP.NET कोर

सभी परियोजना एएसपी.नेट कोर के साथ बनाई गई हैं, लेकिन जेएस क्लाइंट स्थिर फाइलों का उपयोग करते हैं।

मुझे आवश्यकता है कि जेएस क्लाइंट केवल पहचान टोकन (टोकन तक नहीं पहुंचने के साथ) के साथ एपीआई के साथ कनेक्ट हो क्योंकि मुझे केवल एपीआई तक पहुंच की आवश्यकता है, मुझे उपयोगकर्ता स्वचालन का प्रबंधन करने की आवश्यकता नहीं है।

मैं quickstarts पढ़ रहा हूँ पोस्ट https://identityserver4.readthedocs.io/en/dev/quickstarts/1_client_credentials.html

के रूप में मैंने पढ़ा है मैं विचार है कि मैं उपयोगकर्ता के लिए अंतर्निहित ग्रैंड प्रकार की जरूरत है और मैं OpenID Connect ने, केवल OAuth2 आवश्यकता नहीं है।

इसके अलावा मैं इस पोस्ट https://identityserver4.readthedocs.io/en/dev/quickstarts/7_javascript_client.html पढ़ा लेकिन वे टोकन पहुँच का उपयोग करें और मैं की जरूरत है कि, मैं ओआईडीसी-ग्राहक-js उपयोग कर रहा हूँ पुस्तकालय https://github.com/IdentityModel/oidc-client-js एपीआई से कनेक्ट करने और मैं जिस तरह से अंतर्निहित ग्रैंड प्रकार के साथ उपयोग करने के लिए खोज न लेकिन मेरे द्वारा उपयोग की जाने वाली विधियों को मुझे http://localhost:5000/connect/authorize पृष्ठ पर रीडायरेक्ट किया जाता है (मुझे लगता है कि यह तब होता है जब मुझे ओपनआईडी कनेक्ट का उपयोग करने की आवश्यकता होती है)

इसे प्राप्त करने का सबसे अच्छा तरीका क्या है? मुझे क्या गलत है? मैं एपीआई के साथ कैसे autenticate और http://localhost:5001/values

IdentityServer परियोजना

कॉल कर सकते हैं Config.cs

public static IEnumerable<Client> GetClients() 
     { 
      return new List<Client> 
      { 
       new Client 
       { 
        ClientId = "client", 
        ClientName = "JavaScript Client", 
        // no interactive user, use the clientid/secret for authentication 
        AllowedGrantTypes = GrantTypes.Implicit, 
        AllowAccessTokensViaBrowser = true, 



        RedirectUris = new List<string> 
        { 
         "http://localhost:5003/oidc-client-sample-callback.html" 
        }, 
        AllowedCorsOrigins = new List<string> 
        { 
         "http://localhost:5003" 
        }, 

        // scopes that client has access to 
        AllowedScopes = new List<string> 
        { 
         "api1" 
        } 
       } 
      }; 
     } 

Startup.cs

public void ConfigureServices(IServiceCollection services) 
    { 
     // configure identity server with in-memory stores, keys, clients and scopes 
     services.AddDeveloperIdentityServer() 
      .AddInMemoryScopes(Config.GetScopes()) 
      .AddInMemoryClients(Config.GetClients()); 
    } 

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
    { 
     loggerFactory.AddConsole(LogLevel.Debug); 
     app.UseDeveloperExceptionPage(); 

     app.UseIdentityServer(); 
    } 

API प्रोजेक्ट

Startup.cs

public void ConfigureServices(IServiceCollection services) 
{ 

    // Add framework services. 
    services.AddMvc(); 

    services.AddSingleton<ITodoRepository, TodoRepository>(); 

    services.AddCors(options => 
    { 
     // this defines a CORS policy called "default" 
     options.AddPolicy("default", policy => 
     { 
      policy.WithOrigins("http://localhost:5003") 
       .AllowAnyHeader() 
       .AllowAnyMethod(); 
     }); 
    }); 

    services.AddMvcCore() 
     .AddAuthorization() 
     .AddJsonFormatters(); 


} 

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
{ 
    loggerFactory.AddConsole(Configuration.GetSection("Logging")); 
    loggerFactory.AddDebug(); 

    app.UseCors("default"); 

    app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions 
    { 
     Authority = "http://localhost:5000", 
     ScopeName = "api1", 

     RequireHttpsMetadata = false 
    }); 

    app.UseMvc(); 

} 

ValuesController.cs

[Route("api/[controller]")] 
    [Authorize] 
    public class ValuesController : Controller 
    { 
     // GET api/values 
     [HttpGet] 
     public IEnumerable<string> Get() 
     { 
      return new string[] { "value1", "value3" }; 
     } 

     // GET api/values/5 
     [HttpGet("{id}")] 
     public string Get(int id) 
     { 
      return "value"; 
     } 
} 

जावास्क्रिप्ट ग्राहक परियोजना

ओआईडीसी-ग्राहक-sample.html

<!DOCTYPE html> 
<html> 
<head> 
    <title>oidc-client test</title> 
    <link rel='stylesheet' href='app.css'> 
</head> 
<body> 
    <div> 
     <a href='/'>home</a> 
     <a href='oidc-client-sample.html'>clear url</a> 
     <label> 
      follow links 
      <input type="checkbox" id='links'> 
     </label> 
     <button id='signin'>signin</button> 
     <button id='processSignin'>process signin response</button> 
     <button id='signinDifferentCallback'>signin using different callback page</button> 
     <button id='signout'>signout</button> 
     <button id='processSignout'>process signout response</button> 
    </div> 

    <pre id='out'></pre> 

    <script src='oidc-client.js'></script> 
    <script src='log.js'></script> 
    <script src='oidc-client-sample.js'></script> 
</body> 
</html> 

ओआईडीसी-ग्राहक नमूना .js

/////////////////////////////// 
// UI event handlers 
/////////////////////////////// 
document.getElementById('signin').addEventListener("click", signin, false); 
document.getElementById('processSignin').addEventListener("click", processSigninResponse, false); 
document.getElementById('signinDifferentCallback').addEventListener("click", signinDifferentCallback, false); 
document.getElementById('signout').addEventListener("click", signout, false); 
document.getElementById('processSignout').addEventListener("click", processSignoutResponse, false); 
document.getElementById('links').addEventListener('change', toggleLinks, false); 

/////////////////////////////// 
// OidcClient config 
/////////////////////////////// 
Oidc.Log.logger = console; 
Oidc.Log.level = Oidc.Log.INFO; 

var settings = { 
    authority: 'http://localhost:5000/', 
    client_id: 'client', 
    redirect_uri: 'http://localhost:5003/oidc-client-sample-callback.html', 
    response_type: 'token', 
    scope: 'api1' 
}; 
var client = new Oidc.OidcClient(settings); 

/////////////////////////////// 
// functions for UI elements 
/////////////////////////////// 
function signin() { 
    client.createSigninRequest({ data: { bar: 15 } }).then(function (req) { 
     log("signin request", req, "<a href='" + req.url + "'>go signin</a>"); 
     if (followLinks()) { 
      window.location = req.url; 
     } 
    }).catch(function (err) { 
     log(err); 
    }); 
} 
function api() { 
    client.getUser().then(function (user) { 
     var url = "http://localhost:5001/values"; 

     var xhr = new XMLHttpRequest(); 
     xhr.open("GET", url); 
     xhr.onload = function() { 
      log(xhr.status, JSON.parse(xhr.responseText)); 
     } 
     xhr.setRequestHeader("Authorization", "Bearer " + user.access_token); 
     xhr.send(); 
    }); 
} 

ओआईडीसी-ग्राहक-नमूना-callback.html

<!DOCTYPE html> 
<html> 
<head> 
    <title>oidc-client test</title> 
    <link rel='stylesheet' href='app.css'> 
</head> 
<body> 
    <div> 
     <a href="oidc-client-sample.html">back to sample</a> 
    </div> 
    <pre id='out'></pre> 
    <script src='log.js'></script> 
    <script src='oidc-client.js'></script> 
    <script> 
      Oidc.Log.logger = console; 
      Oidc.Log.logLevel = Oidc.Log.INFO; 
      new Oidc.OidcClient().processSigninResponse().then(function(response) { 
       log("signin response success", response); 
      }).catch(function(err) { 
       log(err); 
      }); 
    </script> 
</body> 
</html> 
+0

एपीआई को अज्ञात क्यों नहीं बनाते और काम का गुच्छा क्यों नहीं बचाते? आपको जावास्क्रिप्ट में क्लाइंट आईडी + क्लाइंट सीक्रेट को हार्डकोड करना होगा, जिसका अर्थ है कि उन्हें समझौता किया गया है, जावास्क्रिप्ट में एक रहस्य रखने का कोई तरीका नहीं है। – stevieg

+0

हां, मैं और पढ़ रहा हूं, और मुझे इम्प्लीसिट ग्रैंड टाइप https://aaronparecki.com/2012/07/29/2/oauth2- सिम्प्लिफाइड की आवश्यकता है, लेकिन मुझे नहीं पता कि इसका सही तरीके से उपयोग कैसे किया जाए –

+0

पहचान Server4 में एक सार्वजनिक एपीआई है टाइप करें जो मैं उपयोग करने की कोशिश कर रहा हूं। –

उत्तर

1

जहां तक ​​मैं देख रहा हूँ, अपने कोड काम करना चाहिए, यह सब कुछ करता है।

  1. आपका जावास्क्रिप्ट-ऐप (लोकलहोस्ट: 5003) टोकन (function signin()) का अनुरोध करता है। यह IdentityServer
  2. के लिए पुनः निर्देशित IdentityServer में परिणाम होगा (स्थानीय होस्ट: 5000) की स्थापना की है और ग्राहक सेटिंग्स (Client.cs) अनुरोध मेल खाता है। हालांकि विन्यास उपयोगकर्ताओं और संसाधनों के लिए याद आ रही है: यहाँ देखें: https://github.com/IdentityServer/IdentityServer4.Samples/blob/release/Quickstarts/3_ImplicitFlowAuthentication/src/QuickstartIdentityServer/Startup.cs
  3. आपका जावास्क्रिप्ट एप्लिकेशन के तहत एक सही "लैंडिंग पृष्ठ", एक पृष्ठ जहाँ IdentityServer वापस सफल प्रवेश के बाद पुनर्निर्देश है। ठीक से सेट है और अपने IdentityServer
  4. के खिलाफ अधिकृत करेगा: यह पृष्ठ नए जारी किए गए टोकन ऊपर उठाता है ( new Oidc.OidcClient().processSigninResponse())
  5. आपका जावास्क्रिप्ट एप्लिकेशन के तहत अपनी API-अनुरोध के साथ टोकन (xhr.setRequestHeader("Authorization", "Bearer " + user.access_token);)
  6. आपका एपीआई (5001 स्थानीय होस्ट) भेजता है

तो मुझे लगता है कोड के बारे में सही है, लेकिन शर्तों के संबंध में कुछ गलतफहमी हो।

  • आपको लागू अनुदान की आवश्यकता है। क्लाइंट क्रेडेंशियल के बारे में भूल जाओ, क्योंकि यह किसी अन्य वर्कफ़्लो के लिए डिज़ाइन किया गया है और ब्राउज़र में इसका उपयोग नहीं किया जाना चाहिए, क्योंकि क्लाइंट रहस्य का खुलासा किया जा रहा है। इसका मतलब है कि कोई भी वैध टोकन जारी कर सकता है ("चोरी") और आपकी सुरक्षा से समझौता किया गया है। (यदि आप चाहिए उपयोग ClientCredentials, एक सर्वर प्रॉक्सी विधि बनाएँ)।
  • आप दोनों जगह OpenID Connect (ओआईडीसी) और OAuth2 की जरूरत है। (ये सटीक परिभाषाएं नहीं हैं !!) ओआईडीसी आपके लिए टोकन जारी करता है ("उपयोगकर्ता को लॉग इन करता है"), जबकि ओएथ 2 टोकन को मान्य करता है। चिंता न करें, पहचानकर्ता उस सब की देखभाल करता है।
  • आप पहुँच टोकन की जरूरत है। अनुरोधकर्ता टोकन (आपके लोकहोस्ट: 5003 जावास्क्रिप्ट एप्लिकेशन) के लिए पहचान टोकन जारी किया गया है, एक्सेस टोकन को एपीआई (आपके लोकहोस्ट: 5001 एपीआई)
  • "लॉगिन" के लिए रीडायरेक्ट सामान्य है। यह वेब अनुप्रयोग के लिए सामान्य वर्कफ़्लो है: आप अपना एप्लिकेशन छोड़ देते हैं (लोकलहोस्ट: 5003) और पहचान सर्वर (http://localhost:5000) में समाप्त होता है। सफल लॉगिन के बाद, आपको अपने एप्लिकेशन पर वापस रीडायरेक्ट किया जा रहा है (लोकलहोस्ट: 5003)
संबंधित मुद्दे