با سلام و خسته نباشید
من توی پروژه ام از Jwt استفاده کردم و الان میخوام role base احراز هویت انجام بشه . الان به تمام درخواست های که به سرور ارسال میشه پاسخ 401 داده میشه و میگه که دسترسی ندارید
لطفا بگید مشکل کجاست
کد های Startup
services.AddAuthentication(cfg => { cfg.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme; }) .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, cfg => { // Cookie settings cfg.Cookie.HttpOnly = true; cfg.Cookie.Expiration = TimeSpan.FromDays(150); cfg.LoginPath = "/Account/Login"; // If the LoginPath is not set here, ASP.NET Core will default to /Account/Login cfg.LogoutPath = "/Account/Logout"; // If the LogoutPath is not set here, ASP.NET Core will default to /Account/Logout cfg.AccessDeniedPath = "/Account/AccessDenied"; // If the AccessDeniedPath is not set here, ASP.NET Core will default to /Account/AccessDenied cfg.SlidingExpiration = true; }) .AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, cfg => { cfg.RequireHttpsMetadata = false; cfg.SaveToken = true; cfg.TokenValidationParameters = new TokenValidationParameters() { ValidIssuer = ":Issuer", ValidAudience = "Issuer", IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("myKey")) }; });
و اینم کد تولید token
public async Task<ApiResponse> GenerateToken([FromBody] ApiLoginViewModel model) { ApiResponse apiResponse = new ApiResponse(); if (!ModelState.IsValid) { apiResponse.Data = null; apiResponse.Response.Code = ResponseStateName.BadRequest; apiResponse.Response.Message = "Bad Request"; } else { var user = _userManager.FindByEmailAsync(model.UserName).Result; if (user != null) { var result = _signInManager.CheckPasswordSignInAsync(user, model.Password, false).Result; if (result.Succeeded) { var claims =new List<Claim>() { new Claim(JwtRegisteredClaimNames.Sub, model.UserName), new Claim(JwtRegisteredClaimNames.Jti, user.Id) }; claims.AddRange((await _userManager.GetRolesAsync(user)).Select(r => new Claim(ClaimTypes.Role, r))); var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("myKey")); var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256); var token = new JwtSecurityToken("Issuer", "Issuer", claims, expires: DateTime.Now.AddDays(30), signingCredentials: creds); apiResponse.Data = new { token = new JwtSecurityTokenHandler().WriteToken(token) }; apiResponse.Response.Code = ResponseStateName.Ok; apiResponse.Response.Message = "Ok"; return apiResponse; } } } apiResponse.Data = null; apiResponse.Response.Code = ResponseStateName.SystemError; apiResponse.Response.Message = "Con Not Create Token"; return apiResponse; }
میخوام به صورت زیر استفاده کنم ولی جواب نمیده
[Authorize(Roles="Admin")]
در این لینک کامل توضیح داده دوست من
ممنون استاد ول این مبحثی مد نظر من نیست توی این مقاله از Policy-base استفاده شده من میخوام از role-base استفاده کنم
متاسفانه من تجربه ای در این ضمینه ندارم
امیدوارم دوستانی که تجربه دارند بتونند کمکتون کنند
هیچ کاربری تا کنون از این پست تشکر نکرده است
با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)