ImmortalCAT
@ImmortalCAT
C# loving

Как работает авторизация Identity Server 4 with AspNet Identity?

Вот например дефолтный конфиг IS4
public static class Config
    {
        public static IEnumerable<IdentityResource> IdentityResources =>
        new IdentityResource[]
        {
            new IdentityResources.OpenId(),
            new IdentityResources.Profile(),
        };

        public static IEnumerable<ApiScope> ApiScopes =>
        new ApiScope[]
        {
            new ApiScope("scope1"),
            new ApiScope("scope2"),
            new ApiScope("api1"),
        };

        public static IEnumerable<Client> Clients =>
            new Client[]
            {
                new Client
                {
                    ClientId = "client",

                    // no interactive user, use the clientid/secret for authentication
                    AllowedGrantTypes = GrantTypes.ClientCredentials,

                    // secret for authentication
                    ClientSecrets =
                    {
                        new Secret("secret".Sha256())
                    },

                    // scopes that client has access to
                    AllowedScopes = { "api1" },
                },

                // interactive ASP.NET Core MVC client
                new Client
                {
                    ClientId = "mvc",
                    ClientSecrets = { new Secret("secret".Sha256()) },

                    AllowedGrantTypes = GrantTypes.Code,

                    // where to redirect to after login
                    RedirectUris = { "https://localhost:5001/signin-oidc" },

                    // where to redirect to after logout
                    PostLogoutRedirectUris = { "https://localhost:5001/signout-callback-oidc" },
                    AllowOfflineAccess = true,

                    AllowedScopes = new List<string>
                    {
                        IdentityServerConstants.StandardScopes.OpenId,
                        IdentityServerConstants.StandardScopes.Profile,
                        "api1"
                    }
                },                
            };
    }


Например у меня есть отдельный веб сервер где есть
const string policyAdmin = "WebAppMvc.Policy.Admin";
o.AddPolicy(policyAdmin, policy => {
     policy.RequireClaim(policyAdmin, policyAdmin);
});

А так же я хочу разделять доступ по ролям AspNet Identity
например:
Administration - [Authorize(Policy = Permission.Admin)]
Dashboard - [Authorize(Roles = "Manager")]
Для этого я должен сделать
/// Config.IdentityResources
            new IdentityResource("webApp", new List<string>{ "Admin"})
            /// Config.ApiScope
           new ApiScope("webApp"),
            /// Config.Client
            new Client
                {
                    ClientId = "webApp",
                    ClientSecrets = { new Secret("secret".Sha256()) },

                    AllowedGrantTypes = GrantTypes.Code,

                    // where to redirect to after login
                    RedirectUris = { "https://localhost:5061/signin-oidc" },

                    // where to redirect to after logout
                    PostLogoutRedirectUris = { "https://localhost:5061/signout-callback-oidc" },
                    AllowOfflineAccess = true,

                    AllowedScopes = new List<string>
                    {
                        IdentityServerConstants.StandardScopes.OpenId,
                        IdentityServerConstants.StandardScopes.Profile,
                        "webApp",
                    }
                },

и конфигурация клиента
services.AddAuthentication(options =>
                {
                    options.DefaultScheme = "Cookies";
                    options.DefaultChallengeScheme = "oidc";
                })
                .AddCookie("Cookies")
                .AddOpenIdConnect("oidc", options =>
                {
                    options.Authority = "https://localhost:5051";

                    options.ClientId = "webApp";
                    options.ClientSecret = "secret";
                    options.ResponseType = "code";

                    options.SaveTokens = true;

                    options.Scope.Add("webApp");
                    options.Scope.Add("offline_access");
                });


3 основные модели:
IdentityResource - пользовательские настройки?
ApiScope - что это? некая разрешенная область?
Client - любое приложение для взаимодействия с identity

Плюс добавляется AspNetCore Identity
Как я понимаю роли просто добавляются как claims?
  • Вопрос задан
  • 388 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы