Breaking News

c# – Authorization not working ,ASP.NET Core MVC using API?-Exceptionshub

Questions:

I have two roles admin and employee. Both these users are getting access to the method written below [Authorize(Roles ="Admin")].

This is my code:

[Authorize(Roles ="Admin")]
public async TaskRequestsAsync()
{          
    HttpResponseMessage responseMessage = await client.GetAsync("https://localhost:44347/api/Values/Requests/");

    if (responseMessage.IsSuccessStatusCode)
    {
        List user = JsonConvert.DeserializeObject>(await responseMessage.Content.ReadAsStringAsync());
        ViewBag.StudentModel = user;
        return View(user);
    }
    else
    {
        return RedirectToAction("Error", "Home");
    }
}
enter code here
Here is my startup.cs file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using APIMODULE.Models;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Studentdataproject.Models;

namespace Studentdataproject
{
    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.AddAuthentication
            //   (CookieAuthenticationDefaults.AuthenticationScheme)
            //       .AddCookie();
            services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(op =>
            {
                op.Cookie.Name = "dd";
            });
            services.AddSingleton();            
            services.AddControllersWithViews();

        }

        // 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();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }
            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseCookiePolicy();
            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}

How to&Answers:

About rankhawks

Check Also

Naked C#: A Beginner’s Guide To Coding

Naked C#: A Beginner’s Guide To Coding The bare bones of programming without the bling! …

Build A Real-World App With ASP.NET Core And Angular 2 (4+)

Build a full-stack web app with ASP.NET Core, Entity Framework Core and Angular 2 (Angular …

Learn C# By Building Applications

We will be using Visual Studio Community 2015 (but once Visual Studio gets more updates …

Codeigniter: Learn Concepts Of Codeigniter [WEEKLY UPDATED]

Core PHP is the basic requirement. Computer and the Internet are required to start. Codeigniter …

Real World ASP.NET Web API Services for .NET Framework

Create production-ready, secure, professional Web API services designed for any kind of client integration Bestseller …

Leave a Reply

Your email address will not be published. Required fields are marked *