2016-11-28 11 views
8

के साथ भूमिका आधारित प्रमाणीकरण मैं उपयोगकर्ता भूमिकाओं के आधार पर अपने एपीआई तक पहुंच प्रदान करने के लिए पहचान सर्वर 4 का उपयोग करके "भूमिका आधारित प्राधिकरण" को लागू करने की कोशिश कर रहा हूं।पहचान Server4

उदाहरण के लिए, मैं उपयोगकर्ता के लिए फ्री रोलर और पेडयूसर के लिए दो भूमिकाएं लेना चाहता हूं और [प्राधिकरण (भूमिकाएं = "फ्री यूज़र") का उपयोग करके प्राधिकरण विशेषता के माध्यम से एपीआई तक पहुंच देना चाहता हूं), कृपया मेरी मदद करें कि कैसे क्या मैं इसे प्राप्त कर सकता हूं।

मैं निम्नलिखित समाधान संरचना:

  1. IdentityServer
  2. WebAPI
  3. जावास्क्रिप्ट क्लाइंट

इस प्रकार मैं अपने जावास्क्रिप्ट ग्राहक पंजीकृत किया है:

new Client 
      { 
       ClientId = "js", 
       ClientName = "javascript client", 
       AllowedGrantTypes = GrantTypes.Implicit, 
       AllowAccessTokensViaBrowser= true, 
       RedirectUris = {"http://localhost:5004/callback.html"}, 
       PostLogoutRedirectUris = {"http://localhost:5004/index.html"}, 
       AllowedCorsOrigins = {"http://localhost:5004"}, 

       AllowedScopes = 
       { 
        StandardScopes.OpenId.Name, 
        StandardScopes.Profile.Name, 
        "api1", 
        "role", 
        StandardScopes.AllClaims.Name 
       } 
      } 

कार्यक्षेत्र

return new List<Scope> 
     { 
      StandardScopes.OpenId, 
      StandardScopes.Profile, 

      new Scope 
      { 
       Name = "api1", 
       Description = "My API" 
      }, 
      new Scope 
      { 
       Enabled = true, 
       Name = "role", 
       DisplayName = "Role(s)", 
       Description = "roles of user", 
       Type = ScopeType.Identity, 
       Claims = new List<ScopeClaim> 
       { 
        new ScopeClaim("role",false) 
       } 
      }, 
      StandardScopes.AllClaims 
     }; 

उपयोगकर्ता

return new List<InMemoryUser> 
     { 
      new InMemoryUser 
      { 
       Subject = "1", 
       Username = "alice", 
       Password = "password", 

       Claims = new List<Claim> 
       { 
        new Claim("name", "Alice"), 
        new Claim("website", "https://alice.com"), 
        new Claim("role","FreeUser") 
       } 
      }, 
      new InMemoryUser 
      { 
       Subject = "2", 
       Username = "bob", 
       Password = "password", 

       Claims = new List<Claim> 
       { 
        new Claim("name", "Bob"), 
        new Claim("website", "https://bob.com"), 
        new Claim("role","PaidUser") 
       } 
      } 
     }; 

WebAPI Startup.cs

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
    { 
     loggerFactory.AddConsole(Configuration.GetSection("Logging")); 
     loggerFactory.AddDebug(); 


     JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear(); 
     app.UseCors("default"); 
     app.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions 
     { 
      Authority = "http://localhost:5000", 
      ScopeName = "api1", 
      // AdditionalScopes = new List<string> { "openid","profile", "role" }, 
      RequireHttpsMetadata = false 
     }); 

     app.UseMvc(); 
    } 

वेब एपीआई नियंत्रक

namespace Api.Controllers 
{ 
[Route("[controller]")] 

public class IdentityController : ControllerBase 
{ 
    [HttpGet] 
    [Authorize(Roles = "PaidUser")] 
    public IActionResult Get() 
    { 
     return new JsonResult(from c in User.Claims select new { c.Type, c.Value }); 
    } 

    [Authorize(Roles = "FreeUser")] 
    [HttpGet] 
    [Route("getfree")] 
    public IActionResult GetFreeUser() 
    { 
     return new JsonResult(from c in User.Claims select new { c.Type, c.Value }); 
    } 
} 
} 

जावास्क्रिप्ट क्लाइंट app.js यहाँ मैं IdentityServer के माध्यम से उपयोगकर्ता के लिए लॉग इन और एक API अनुरोध बनाने के लिए कोशिश कर रहा हूँ।

var mgr = new Oidc.UserManager(config); 
mgr.getUser().then(function (user) { 
if (user) { 
    log("User logged in", user.profile); 
} else { 
    log("User is not logged in."); 
} 
}); 

function login() { 
    mgr.signinRedirect(); 
} 

function api() { 
mgr.getUser().then(function (user) { 
    var url = "http://localhost:5001/identity/getfree"; 

    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(); 
    }); 
} 

function logout() { 
    mgr.signoutRedirect(); 
} 

लॉगिन प्रवाह ठीक काम करता है और मैं सफलतापूर्वक लॉगिन कर सकता हूं, और मैं एक्सेस टोकन में भूमिका प्राप्त कर सकता हूं।

enter image description here

जब मैं बटन पर क्लिक करके एपीआई के लिए अनुरोध करने (API कॉल) तो मैं निम्नलिखित त्रुटि मिलती है .. enter image description here

उत्तर

5

यह देखते हुए कि आप जावास्क्रिप्ट ग्राहक के लिए config वस्तु प्रदान नहीं किया है, मैं तुम्हें गुंजाइश इस प्रकार कॉन्फ़िगर किया गया मान।

scope:"openid profile api1 role" 

मेरा मानना ​​है कि आपकी समस्या का मुख्य कारण यह है कि भूमिका का दावा आपके पहुंच टोकन में शामिल नहीं है।

एपीआई 1 स्कोप में भूमिका दावा जोड़ें, इसे एक्सेस टोकन में शामिल करने के लिए निम्नानुसार है।

   new Scope 
       { 
        Name = "api1", 
        DisplayName = "API1 access", 
        Description = "My API", 
        Type = ScopeType.Resource, 
        IncludeAllClaimsForUser = true, 
        Claims = new List<ScopeClaim> 
        { 
         new ScopeClaim(ClaimTypes.Name), 
         new ScopeClaim(ClaimTypes.Role) 
        } 
       } 

आप इस मुद्दे को डीबग करने में सहायता के लिए यहां मेरा उत्तर पढ़ सकते हैं। implementing roles in identity server 4 with asp.net identity

पूरा कार्य समाधान यहां है। https://github.com/weliwita/IdentityServer4.Samples/tree/40844310

+0

महान..यह ठीक काम करता है .. मुझे विश्वास नहीं है कि मैं कैसे भूल गया इसे स्कॉप्स में जोड़ने के लिए .. वैसे भी धन्यवाद .. –

+0

@ मुहम्मदमावाका मुझे लगता है कि आपको जवाब स्वीकार करना चाहिए – Learner

2

new Claim(ClaimTypes.Role, "FreeUser")

को बदलें new Claim("role","FreeUser") या एक बनाने इस तरह की नीति:

services.AddAuthorization(options => 
{ 
    options.AddPolicy("FreeUser", policy => policy.RequireClaim("role", "FreeUser")); 
}); 

और हम यह ई:

Authorize[(Policy = "FreeUser")] 
+0

आपकी प्रतिक्रिया के लिए धन्यवाद, मैंने दोनों तरीकों का प्रयास किया है लेकिन परिणाम एक ही है, जब मैं एपीआई अनुरोध करता हूं तो नीति जोड़ने के मामले में, यह एपीआई को दो अनुरोध करता है, पहला व्यक्ति 204 [कोई सामग्री] स्थिति नहीं कहता है कोड जबकि दूसरा वाला 403 के साथ असफल हो जाता है .. दावे के मामले में परिणाम फिर से 403 है .. क्या मुझे कुछ याद आ रहा है? .. –

0

मैं इस पोस्ट

Identity Server 4: adding claims to access token

मैं भूमिकाओं और दावे के साथ परीक्षण किया है भी मैं दोनों ग्राहक वेब अनुप्रयोग में उपयोग कर सकते हैं [अधिकृत (भूमिका = "SuperAdmin, व्यवस्थापक")] पर एक नमूना लिखा था और एपीआई ऐप।

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