项目作者: Kasunjith-Bimal

项目描述 :
Identity Server with Database
高级语言: C#
项目地址: git://github.com/Kasunjith-Bimal/TimeStone.IdentityServer.EntityFramwork.git


TimeStone.IdentityServer.EntityFramwork

Identity Server with Database

Identity Server Configuration

Go https://github.com/Kasunjith-Bimal/TimeStone.IdentityServer.EntityFramwork/blob/master/TimeStone.IdentityServer/Config.cs

Replace your MVC application Url - http://localhost:3728 (Change RedirectUris and PostLogoutRedirectUris)
Replace your Angualr application Url - http://localhost:4200 (Change RedirectUris and PostLogoutRedirectUris)

  1. public static IEnumerable<Client> GetClients()
  2. {
  3. return new[]
  4. {
  5. new Client
  6. {
  7. ClientId ="mvc",
  8. ClientName="Mvc Demo",
  9. AllowedGrantTypes = GrantTypes.Hybrid,
  10. RedirectUris ={ "http://localhost:3728/signin-oidc" },
  11. AllowedScopes={ "openid","email","profile","API1"},
  12. PostLogoutRedirectUris = { "http://localhost:3728/signout-callback-oidc" },
  13. ClientSecrets ={new Secret("secret".Sha256()) }
  14. },
  15. new Client {
  16. RequireConsent = false,
  17. ClientId = "angular_spa",
  18. ClientName = "Angular SPA",
  19. AllowedGrantTypes = GrantTypes.Implicit,
  20. AllowedScopes = { "openid", "profile", "email", "API1" },
  21. RedirectUris = {"http://localhost:4200/callback.html"},
  22. PostLogoutRedirectUris = {"http://localhost:4200/signout-callback.html"},
  23. AllowedCorsOrigins = {"http://localhost:4200"},
  24. AllowAccessTokensViaBrowser = true,
  25. AccessTokenLifetime = 3600,
  26. }
  27. };
  28. }

Go https://github.com/Kasunjith-Bimal/TimeStone.IdentityServer.EntityFramwork/blob/master/TimeStone.Mvc/Startup.cs

Replace your Identity Server Url - http://localhost:5000 (Change options.Authority = “http://localhost:5000“;)

  1. services.AddAuthentication(options =>
  2. {
  3. options.DefaultScheme = "Cookies";
  4. options.DefaultChallengeScheme = "oidc";
  5. }).AddCookie("Cookies")
  6. .AddOpenIdConnect("oidc", options =>
  7. {
  8. options.SignInScheme = "Cookies";
  9. options.RequireHttpsMetadata = false;
  10. options.Authority = "http://localhost:5000";
  11. options.ClientId = "mvc";
  12. options.ClientSecret = "secret";
  13. options.ResponseType = "code id_token";
  14. options.Scope.Add("openid");
  15. options.Scope.Add("email");
  16. options.Scope.Add("profile");
  17. options.Scope.Add("API1");
  18. options.SaveTokens = true;
  19. });

Go https://github.com/Kasunjith-Bimal/TimeStone.IdentityServer.EntityFramwork/blob/master/TimeStone.Api/Startup.cs

Replace your Identity Server Url - http://localhost:5000 (Change options.Authority = “http://localhost:5000“;)

  1. services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
  2. .AddJwtBearer(options =>
  3. {
  4. options.Audience = "API1";
  5. options.Authority = "http://localhost:5000";
  6. options.RequireHttpsMetadata = false;
  7. });

Restore Application

  1. dotnet restore

Database Configuration

Go TimeStone.IdentityServer.EntityFramwork/TimeStone.IdentityServer/appsettings.json

Change Server Name and Database Name According to your Sql Server Configuration

  1. {
  2. "ConnectionStrings": {
  3. "DefaultConnection": "Data Source=ServerName;Initial Catalog=DataBase;Integrated Security=True"
  4. }
  5. }

Update database (Go Package Manager Console - Select TimeStone.IdentityServer)

  1. dotnet ef database update --context ApplicationDbContext
  1. dotnet ef database update --context ConfigurationDbContext
  1. dotnet ef database update --context PersistedGrantDbContext

Go TimeStone.IdentityServer.EntityFramwork/TimeStone.IdentityServer/Startup.cs

comments InitializeDatabase(app); line

  1. //InitializeDatabase(app);

Run Seed Data (Save Default user Data)

  1. dotnet run /seed

Remove comments InitializeDatabase(app); line

  1. InitializeDatabase(app);

Run Application