C# 如何使用授权blazor?
我在blazor中使用身份验证和授权时遇到问题, 我得到的唯一瘦消息是客户机机密、客户机id和客户机密钥。我不知道如何在blazor上实现它 这是我的密码 appsetting.jsonC# 如何使用授权blazor?,c#,blazor,C#,Blazor,我在blazor中使用身份验证和授权时遇到问题, 我得到的唯一瘦消息是客户机机密、客户机id和客户机密钥。我不知道如何在blazor上实现它 这是我的密码 appsetting.json "AllowedHosts": "*", "Auth0": { "Domain": "https://sso.*****.com/login", "ClientI
"AllowedHosts": "*",
"Auth0": {
"Domain": "https://sso.*****.com/login",
"ClientId": "*****************",
"ClientSecret": "**************"
}
这是我在startup.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using BookingCrewsUI.Data;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Authentication.Cookies;
namespace BookingCrewsUI
{
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.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddServerSideBlazor();
services.Configure<CookiePolicyOptions>(options =>
{
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
}
);
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
}
).AddCookie()
.AddOpenIdConnect("Auth0", options =>
{
options.Authority = $"https://{Configuration["Auth0:Domain"]}";
options.ClientId = Configuration["Auth0:ClientId"];
options.ClientSecret = Configuration["Auth0:ClientSecret"];
options.ResponseType = "code";
options.Scope.Clear();
options.Scope.Add("openid");
options.CallbackPath = new PathString("/callback");
options.ClaimsIssuer = "Auth0";
options.Events = new OpenIdConnectEvents
{
OnRedirectToIdentityProviderForSignOut = (context)
=>
{
var logoutUri = $"https://{Configuration["Auth0:Domain"]}/v2/logout?client_id={Configuration["Auth0:ClientId"]}";
var postLogoutUri = context.Properties.RedirectUri;
if (!string.IsNullOrEmpty(postLogoutUri))
{
if (postLogoutUri.StartsWith("/"))
{
// transform to absolute
var request = context.Request;
postLogoutUri = request.Scheme + "://" + request.Host + request.PathBase + postLogoutUri;
}
logoutUri += $"&returnTo={ Uri.EscapeDataString(postLogoutUri)}";
}
context.Response.Redirect(logoutUri);
context.HandleResponse();
return Task.CompletedTask;
}
};
})
;
services.AddHttpContextAccessor();
services.AddSingleton<WeatherForecastService>();
}
// 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("/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.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
使用Microsoft.AspNetCore.Builder;
使用Microsoft.AspNetCore.Components;
使用Microsoft.AspNetCore.Hosting;
使用Microsoft.AspNetCore.HttpsPolicy;
使用Microsoft.Extensions.Configuration;
使用Microsoft.Extensions.DependencyInjection;
使用Microsoft.Extensions.Hosting;
使用BookingCrewsUI.Data;
使用Microsoft.AspNetCore.Http;
使用Microsoft.AspNetCore.Authentication.OpenIdConnect;
使用Microsoft.AspNetCore.Authentication.Cookies;
命名空间BookingCrewsUI
{
公营创业
{
公共启动(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
//此方法由运行时调用。请使用此方法将服务添加到容器中。
//有关如何配置应用程序的更多信息,请访问https://go.microsoft.com/fwlink/?LinkID=398940
public void配置服务(IServiceCollection服务)
{
services.AddRazorPages();
AddServerSideBlazor();
配置(选项=>
{
options.checkApprovered=context=>true;
options.MinimumSameSitePolicy=SameSiteMode.None;
}
);
services.AddAuthentication(选项=>
{
options.DefaultAuthenticateScheme=CookieAuthenticationDefaults.AuthenticationScheme;
options.defaultsignnscheme=CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme=CookieAuthenticationDefaults.AuthenticationScheme;
}
).AddCookie()
.AddOpenIdConnect(“Auth0”,选项=>
{
options.Authority=$“https://{Configuration[“Auth0:Domain”]}”;
options.ClientId=配置[“Auth0:ClientId”];
options.ClientSecret=配置[“Auth0:ClientSecret”];
options.ResponseType=“code”;
options.Scope.Clear();
options.Scope.Add(“openid”);
options.CallbackPath=新路径字符串(“/callback”);
options.ClaimsIssuer=“Auth0”;
options.Events=新的OpenIdConnectEvents
{
OnRedirectToIdentityProviderForSignOut=(上下文)
=>
{
var logoutUri=$“https://{Configuration[“Auth0:Domain”]}/v2/logout?client_id={Configuration[“Auth0:ClientId”]}”;
var postLogoutUri=context.Properties.RedirectUri;
如果(!string.IsNullOrEmpty(postLogoutUri))
{
if(postLogoutUri.StartsWith(“/”)
{
//转化为绝对
var-request=context.request;
postLogoutUri=request.Scheme+“:/”+request.Host+request.PathBase+postLogoutUri;
}
logoutUri+=$“&returnTo={Uri.EscapeDataString(postLogoutUri)}”;
}
重定向(logoutUri);
context.HandleResponse();
返回Task.CompletedTask;
}
};
})
;
AddHttpContextAccessor();
services.AddSingleton();
}
//此方法由运行时调用。请使用此方法配置HTTP请求管道。
public void配置(IApplicationBuilder应用程序、IWebHostEnvironment环境)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
其他的
{
app.UseExceptionHandler(“/Error”);
//默认的HSTS值为30天。您可能希望在生产场景中更改此值,请参阅https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseCookiePolicy();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(端点=>
{
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage(“/_主机”);
});
}
}
}
这里是我包括授权,什么都没有发生
Index.razor
@page "/"
@attribute [Authorize]
<h1>Hello, world!</h1>
Welcome to your new app.
<SurveyPrompt Title="How is Blazor working for you?" />
@code
{
}
@page/“
@属性[授权]
你好,世界!
欢迎使用您的新应用程序。
@代码
{
}
我不知道我的代码出了什么问题,我想当我点击
localhost:5001
时,我会在我的应用程序json中将登录重定向到sso url,但什么都没有发生,有人能帮我吗?已经没有发生什么事情是的,什么都没有发生我想要的是当localhost 5001启动时,我会被重定向到登录