授权不使用Azure SQL的角色

时间:2019-09-05 03:37:31

标签: c# asp.net-core azure-sql-database asp.net-core-webapi asp.net-authorization

首先,这是我的控制器

[Authorize(Roles = Utilities.BusinessHead+","+Utilities.DeliveryHeadNoida)]
[HttpDelete("{id}")]
public IActionResult DeleteCustomer(int id)
{
    var customer = _customerService.CustomerDetails(id);
    if (customer == null)
    {
        return NotFound(Utilities.NoData);
    }
    _customerService.DeleteCustomer(id);
    return Ok(Utilities.DataDeleted);
}

实用程序类包含:

public static class Utilities
    {

        public const string PmoNoida = "PMO Noida";
        public const string ApplicationAdmin = "Application Admin";
        public const string DeliveryHeadNoida = "Delivery Head Noida";
        public const string DeliveryManagerYvr = "Delivery Manager YVR";
        public const string BusinessHead = "Business Head ";
        public const string SalesManager = "Sales Manager";
        public const string DataDeleted = "Data deleted";

    }

startup.cs类似,

 public class Startup
    {
        public IConfiguration Configuration { get; }
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        /// <summary>
        /// This method gets called by the runtime. Use this method to add services to the container. 
        /// </summary>         
        public void ConfigureServices(IServiceCollection services)
        {
            var key = Encoding.UTF8.GetBytes(Utilities.SecretKey);
            services.AddAuthentication(x =>
            {
                x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(x =>
            {
                x.RequireHttpsMetadata = false;
                x.SaveToken = true;
                x.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey = new SymmetricSecurityKey(key),
                    ValidateIssuer = false,
                    ValidateAudience = false
                };
            });
            services.AddDbContext<KickOffContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
            services.AddScoped<ICustomersService, CustomersService>();
            services.AddScoped<ICustomerRepository, CustomersRepository>();
            services.AddScoped<ILoginService, LoginService>();
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc(Utilities.SwaggerDoc, new Info { Title = Utilities.SwaggerTitle, Version = Utilities.SwaggerVersion });
            });
            services.AddMvc()
            .AddJsonOptions(options =>
            {
                options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
            });
            var corsBuilder = new CorsPolicyBuilder();
            corsBuilder.AllowAnyHeader();
            corsBuilder.AllowAnyMethod();
            corsBuilder.AllowAnyOrigin(); // For anyone access.            
            corsBuilder.AllowCredentials();
            services.AddCors(options =>
            {
                options.AddPolicy(Utilities.SiteCorsPolicy, corsBuilder.Build());
            });
        }

        /// <summary>
        /// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        /// </summary>        
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {           
            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint(Utilities.SwaggerEndPoint, Utilities.SwaggerApiVersion);
            });
            using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
            {
                var context = serviceScope.ServiceProvider.GetRequiredService<KickOffContext>();
                context.Database.EnsureCreated();
            }
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseHsts();
            }
            app.UseHttpsRedirection();
            app.UseAuthentication();
            app.UseMvc();
            app.UseStatusCodePages();
            app.UseCors(Utilities.SiteCorsPolicy);
        }
    }

当我尝试删除具有角色的用户时遇到问题:
 当我使用Azure SQL数据库时,“业务负责人”说"403 Forbidden"
 但是当我使用SQL Server Express时,一切都很好。
为什么它不能与Azure SQL一起使用?

1 个答案:

答案 0 :(得分:0)

将此代码添加到export class AppComponent { courses: AngularFireList<string[]>; courses$: Observable<any[]> constructor(private db: AngularFireDatabase) { } ngOnInit() { this.courses = this.db.list('/courses'); this.courses$ = this.courses .valueChanges() } addCourse(course: HTMLInputElement) { this.courses.push([course.value]); course.value = ''; } } 可能会解决此问题。

web.config