API
یک پروژه ی API بسازید طراحی اپلیکیشن و کلاس Startup آن را رایا پارس ربه روزرسانی نمایید.
public class Startup
{
public void ConfigureServices(
IServiceCollection services)
{
services.AddAuthentication(
IdentityServerAuthenticationDefaults.JwtAuthenticationScheme)
.AddIdentityServerAuthentication(options =>
{
options.Authority = "http://localhost:5000"; // Auth Server
options.RequireHttpsMetadata = false; // only for development
options.ApiName = "fiver_auth_api"; // API Resource Id
});
services.AddMvc();
}
public void Configure(
IApplicationBuilder app,
IHostingEnvironment env)
{
app.UseAuthentication();
app.UseMvcWithDefaultRoute();
}
}
یک Controller بسازید و با به کارگیری از خصوصیت [Authorize] از آن حفظ فرمایید.
[Authorize]
[Route("movies")]
public class MoviesController : Controller
{
Client
یک اینترنت نرم افزار بسازید و کلاس Startup آن را آپ دیت رسانی نمایید.
public class Startup
{
public void ConfigureServices(
IServiceCollection services)
{
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
services.AddAuthentication(options =>
{
options.DefaultScheme =
CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme =
OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie()
.AddOpenIdConnect(options =>
{
options.SignInScheme =
CookieAuthenticationDefaults.AuthenticationScheme;
options.Authority = "http://localhost:5000"; // Auth Server
options.RequireHttpsMetadata = false; // only for development
options.ClientId = "fiver_auth_client"; // client setup in Auth Server
options.ClientSecret = "secret";
options.ResponseType = "code id_token"; // means Hybrid flow
options.Scope.Add("fiver_auth_api");
options.Scope.Add("offline_access");
options.GetClaimsFromUserInfoEndpoint = true;
options.SaveTokens = true;
});
services.AddMvc();
}
public void Configure(
IApplicationBuilder app,
IHostingEnvironment env)
{
app.UseAuthentication();
app.UseMvcWithDefaultRoute();
}
}