के साथ भूमिका आधारित प्रमाणीकरण मैं उपयोगकर्ता भूमिकाओं के आधार पर अपने एपीआई तक पहुंच प्रदान करने के लिए पहचान सर्वर 4 का उपयोग करके "भूमिका आधारित प्राधिकरण" को लागू करने की कोशिश कर रहा हूं।पहचान Server4
उदाहरण के लिए, मैं उपयोगकर्ता के लिए फ्री रोलर और पेडयूसर के लिए दो भूमिकाएं लेना चाहता हूं और [प्राधिकरण (भूमिकाएं = "फ्री यूज़र") का उपयोग करके प्राधिकरण विशेषता के माध्यम से एपीआई तक पहुंच देना चाहता हूं), कृपया मेरी मदद करें कि कैसे क्या मैं इसे प्राप्त कर सकता हूं।
मैं निम्नलिखित समाधान संरचना:
- IdentityServer
- WebAPI
- जावास्क्रिप्ट क्लाइंट
इस प्रकार मैं अपने जावास्क्रिप्ट ग्राहक पंजीकृत किया है:
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();
}
लॉगिन प्रवाह ठीक काम करता है और मैं सफलतापूर्वक लॉगिन कर सकता हूं, और मैं एक्सेस टोकन में भूमिका प्राप्त कर सकता हूं।
जब मैं बटन पर क्लिक करके एपीआई के लिए अनुरोध करने (API कॉल) तो मैं निम्नलिखित त्रुटि मिलती है ..
महान..यह ठीक काम करता है .. मुझे विश्वास नहीं है कि मैं कैसे भूल गया इसे स्कॉप्स में जोड़ने के लिए .. वैसे भी धन्यवाद .. –
@ मुहम्मदमावाका मुझे लगता है कि आपको जवाब स्वीकार करना चाहिए – Learner