Salight
Salight
CC#
Created by Salight on 1/7/2025 in #help
I got a Mvc and an a API project i want to use [Authorize] attribute in mvc is it possible?
now it authorizes
22 replies
CC#
Created by Salight on 1/7/2025 in #help
I got a Mvc and an a API project i want to use [Authorize] attribute in mvc is it possible?
i was thinking i am getting the data from appsettings but i forget the add configure jwt settings service
22 replies
CC#
Created by Salight on 1/7/2025 in #help
I got a Mvc and an a API project i want to use [Authorize] attribute in mvc is it possible?
you were right
22 replies
CC#
Created by Salight on 1/7/2025 in #help
I got a Mvc and an a API project i want to use [Authorize] attribute in mvc is it possible?
okay bro i solved
22 replies
CC#
Created by Salight on 1/7/2025 in #help
I got a Mvc and an a API project i want to use [Authorize] attribute in mvc is it possible?
it is not actually
22 replies
CC#
Created by Salight on 1/7/2025 in #help
I got a Mvc and an a API project i want to use [Authorize] attribute in mvc is it possible?
you mean audience
22 replies
CC#
Created by Salight on 1/7/2025 in #help
I got a Mvc and an a API project i want to use [Authorize] attribute in mvc is it possible?
context.Exception.Message = "IDX10208: Unable to validate audience. validationParameters.ValidAudience is null or whitespace and validationParameters.ValidAudiences is null."
context.Exception.Message = "IDX10208: Unable to validate audience. validationParameters.ValidAudience is null or whitespace and validationParameters.ValidAudiences is null."
this is my exception btw
22 replies
CC#
Created by Salight on 1/7/2025 in #help
I got a Mvc and an a API project i want to use [Authorize] attribute in mvc is it possible?
like this
22 replies
CC#
Created by Salight on 1/7/2025 in #help
I got a Mvc and an a API project i want to use [Authorize] attribute in mvc is it possible?
public static void ConfigureJWT(this IServiceCollection services, IConfiguration
configuration)
{
var jwtConfiguration = new JwtConfiguration();
configuration.Bind(jwtConfiguration.Section, jwtConfiguration);
var secretKey = Environment.GetEnvironmentVariable("SECRET");
services.AddAuthentication(opt =>
{
opt.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
opt.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,

ValidIssuer = jwtConfiguration.ValidIssuer,
ValidAudience = jwtConfiguration.ValidAudience,
IssuerSigningKey = new
SymmetricSecurityKey(Encoding.UTF8.GetBytes(secretKey))
};
options.Events = new JwtBearerEvents
{
OnAuthenticationFailed = context =>
{
Console.WriteLine("Authentication failed: " + context.Exception.Message);
return Task.CompletedTask;
},
OnTokenValidated = context =>
{
Console.WriteLine("Token validated: " + context.SecurityToken);
return Task.CompletedTask;
}
};
});
public static void ConfigureJWT(this IServiceCollection services, IConfiguration
configuration)
{
var jwtConfiguration = new JwtConfiguration();
configuration.Bind(jwtConfiguration.Section, jwtConfiguration);
var secretKey = Environment.GetEnvironmentVariable("SECRET");
services.AddAuthentication(opt =>
{
opt.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
opt.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,

ValidIssuer = jwtConfiguration.ValidIssuer,
ValidAudience = jwtConfiguration.ValidAudience,
IssuerSigningKey = new
SymmetricSecurityKey(Encoding.UTF8.GetBytes(secretKey))
};
options.Events = new JwtBearerEvents
{
OnAuthenticationFailed = context =>
{
Console.WriteLine("Authentication failed: " + context.Exception.Message);
return Task.CompletedTask;
},
OnTokenValidated = context =>
{
Console.WriteLine("Token validated: " + context.SecurityToken);
return Task.CompletedTask;
}
};
});
22 replies
CC#
Created by Salight on 1/7/2025 in #help
I got a Mvc and an a API project i want to use [Authorize] attribute in mvc is it possible?
i am using jwtbearer
22 replies
CC#
Created by Salight on 1/7/2025 in #help
I got a Mvc and an a API project i want to use [Authorize] attribute in mvc is it possible?
what is the existing handler i am not aware of that
22 replies
CC#
Created by Salight on 1/7/2025 in #help
I got a Mvc and an a API project i want to use [Authorize] attribute in mvc is it possible?
public async Task<LoginResponse> AuthenticateUser(LoginRequesting userDetails)
{
HttpResponseMessage response = await _httpClient.PostAsJsonAsync("authentication/login", userDetails);
response.EnsureSuccessStatusCode();

var contents = await response.Content.ReadAsStringAsync();
var APIResponse = JsonConvert.DeserializeObject<LoginResponse>(contents);
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", APIResponse.AccessToken);
return APIResponse;

}
public async Task<LoginResponse> AuthenticateUser(LoginRequesting userDetails)
{
HttpResponseMessage response = await _httpClient.PostAsJsonAsync("authentication/login", userDetails);
response.EnsureSuccessStatusCode();

var contents = await response.Content.ReadAsStringAsync();
var APIResponse = JsonConvert.DeserializeObject<LoginResponse>(contents);
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", APIResponse.AccessToken);
return APIResponse;

}
22 replies
CC#
Created by Salight on 1/7/2025 in #help
I got a Mvc and an a API project i want to use [Authorize] attribute in mvc is it possible?
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginRequesting UserModel)
{
//var loginResponse = await _apiClient.LoginAsync<LoginRequesting, LoginResponse>("https://localhost:5001/api/authentication/login", UserModel);
var obj= await _apiClient.AuthenticateUser(UserModel);
if (obj != null && obj.AccessToken.ToString() != "")
{

HttpContext.Session.SetString("AuthToken", obj.AccessToken);
HttpContext.Session.SetString("RefreshToken", obj.RefreshToken);
return RedirectToAction("Index", "Home");

}
else
{
HttpContext.Session.SetString("JWTToken", "");
HttpContext.Session.SetString("RefreshToken", "");
}
return View(obj);
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginRequesting UserModel)
{
//var loginResponse = await _apiClient.LoginAsync<LoginRequesting, LoginResponse>("https://localhost:5001/api/authentication/login", UserModel);
var obj= await _apiClient.AuthenticateUser(UserModel);
if (obj != null && obj.AccessToken.ToString() != "")
{

HttpContext.Session.SetString("AuthToken", obj.AccessToken);
HttpContext.Session.SetString("RefreshToken", obj.RefreshToken);
return RedirectToAction("Index", "Home");

}
else
{
HttpContext.Session.SetString("JWTToken", "");
HttpContext.Session.SetString("RefreshToken", "");
}
return View(obj);
22 replies
CC#
Created by Salight on 11/1/2024 in #help
How can i connect to Supabase Database with my .net Api
you helped me a lot
48 replies
CC#
Created by Salight on 11/1/2024 in #help
How can i connect to Supabase Database with my .net Api
okay then i will dig into the first problem thank you a lot bud
48 replies
CC#
Created by Salight on 11/1/2024 in #help
How can i connect to Supabase Database with my .net Api
then also my appsettings json is in secure too right ?
48 replies
CC#
Created by Salight on 11/1/2024 in #help
How can i connect to Supabase Database with my .net Api
so is it secure to connect the database with connection string?
48 replies