- 1400/05/03
- 0
- 216
مشکل با احراز هویت jwt
من پروژه ی فصل آخر دوره ستارگان asp.netcore رو پیاده سازی کردم در بخش انتهای وقسمت web api تلاش کردم که jwt رو پیاده سازی کنم ... توکن ایجاد می شود اما در بخش startup احراز هویت ساخته نمی شود ...
در زیر کدهای استارت آپ رو مشاهده می کنید....
using Application.Interfaces.Contexts;
using Application.Interfaces.IEmailSmsFacads;
using Application.Services.Mongodb;
using Domain.Users;
using Infrastructure.ExternalApi.ImageServer;
using Infrastructure.IdentityConfigs;
using Infrastructure.SwaggerConfigs;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using Persistence.Contexts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BlueLink.WebApi
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
#region Connection String
string connectionstring = Configuration["ConnectionStrings:SqlServer"];
services.AddDbContext<DataBaseContext>(options => options.UseSqlServer(connectionstring));
services.AddIdentityService(Configuration);
// Adding Authentication
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
})
// Adding Jwt Bearer
.AddJwtBearer(options =>
{
options.SaveToken = true;
options.RequireHttpsMetadata = false;
options.TokenValidationParameters = new TokenValidationParameters()
{
ValidateIssuer = true,
ValidateAudience = true,
ValidAudience = Configuration["JwtConfig:audience"],
ValidIssuer = Configuration["JwtConfig:issuer"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["JwtConfig:key"])),
};
});
#endregion
#region Facad
services.AddTransient<IEmailSmsFacad, EmailSmsFacad>();
#endregion
#region Add Services
services.AddScoped<IDataBaseContext, DataBaseContext>();
services.AddScoped<IIdentityDataBaseContext, IdentityDataBaseContext>();
services.AddScoped<IImageUploadService, ImageUploadService>();
#endregion
#region Swagger
services.AddSwaggerService(Configuration);
#endregion
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "BlueLink.WebApi v1"));
//app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v2/swagger.json", "EndPoint.WebApi v2"));
}
else
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "BlueLink.WebApi v1"));
//app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v2/swagger.json", "EndPoint.WebApi v2"));
}
//app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
پاسخی برای این باگ ارسال کنید
لطفا کدها را درون قطعه کد ارسال نمایید
برای ارسال پاسخ باید وارد حساب کاربری خود شوید
ورود به حساب کاربری