从Visual Studio的"Web API"项目模板开始,我试图将自定义声明添加到/Api/Account/ExternalLogin端点创建的令牌.我通过FacebookAuthenticationProvider.OnAuthenticated回调添加这些,但它们不会持续到OAuthAuthorizationServerProvider.AuthorizationEndpointResponse().
注意:我使用Rahul Nath在他的文章ASP.NET Web API和外部登录中记录的类似方法- 使用社交网络进行身份验证
在我的Startup.Auth.cs类的ConfigureAuth()方法(从OwinStartup类的Configuration()方法调用)中,我向OnAuthenticated属性添加了一个回调函数,以便设置单个声明,foo其值为bar:
var facebookAuthenticationProvider = new FacebookAuthenticationProvider() {
OnAuthenticated = (context) => {
context.Identity.AddClaim(new Claim("foo", "bar"));
return Task.FromResult(0);
}
};
Run Code Online (Sandbox Code Playgroud)
然后我将FacebookAuthenticationProvider实例添加到一个新FacebookAuthenticationOptions对象:
var facebookAuthenticationOptions = new FacebookAuthenticationOptions() {
AppId = "XXXX",
AppSecret = "YYYY",
Provider = facebookAuthenticationProvider
};
Run Code Online (Sandbox Code Playgroud)
并将其传递给OWIN的UseFacebookAuthentication()方法:
app.UseFacebookAuthentication(facebookAuthenticationOptions);
Run Code Online (Sandbox Code Playgroud)
如果我在OnAuthenticated回调中放置一个断点,我可以看到我的自定义声明被添加,以及许多其他声明(包括 …
facebook asp.net-web-api asp.net-identity asp.net-web-api2 owin-middleware
ASP.NET Core 1.1 中引入了将视图组件作为标记帮助程序调用。(请参阅“将视图组件作为标签助手调用”)。但以下仅返回视图的VC部分的测试。似乎该<vc:annual-orders>…</vc:annual-orders>部分根本没有被调用。
视图\共享\组件\AnnualOrders\Default.cshtml:
@{
Layout = "";
}
<div>Test for VC</div>
<div>
<vc:annual-orders>
</vc:annual-orders>
</div>
Run Code Online (Sandbox Code Playgroud)
myProj\ViewComponents\AnnualOrdersViewComponent.cs:
public class AnnualOrdersViewComponent : ViewComponent
{
private readonly OrdersContext _context;
public AnnualOrdersViewComponent(OrdersContext context)
{
_context = context;
}
public async Task<IViewComponentResult> InvokeAsync()
{
var lastOrders = _context.Where(t => t.orderType == "new");
return View(await lastOrders);
}
}
Run Code Online (Sandbox Code Playgroud)
笔记:
c# asp.net-core asp.net-core-1.1 asp.net-core-tag-helpers asp.net-core-viewcomponent
在 ASP.NET Core 2.2 中,我可以设置UseWebRoot()为:
public static void Main(string[] args)
{
CreateWebHostBuilder(args)
.UseUrls("http://*:5000")
.UseWebRoot(@".\WebSite\wwwroot\")
.Build()
.Run();
}
Run Code Online (Sandbox Code Playgroud)
但是我今天不知道该怎么做,因为已经没有这样的方法了。
我刚刚使用最新的 .NET Core 3.1.2 将 Visual Studio 更新到最新的 16.6.0 Preview 1.0。我的项目有<Nullable>enable</Nullable>.
似乎发生了变化IEquatable<T>,现在定义为[AllowNull]:
public interface IEquatable<T>
{
bool Equals([AllowNull] T other);
}
Run Code Online (Sandbox Code Playgroud)
其结果是,任何类矿山其中实施的IEquatable<T>与bool Equals(T o)现在显示一个警告:
CS8767:由于可空性属性,“bool MyType.Equals(MyType o)”的参数“o”类型中的引用类型的可空性与隐式实现的成员 'bool IEquatable.Equals(MyType other)' 不匹配。
解决这个问题的最佳方法是什么?
我可以添加一个可为空的 operator bool Equals(T? o),或者我可以添加[AllowNull](参考System.Diagnostics.CodeAnalysis)。两者似乎都使警告消失,但我不确定哪个更好。
此外,我现在为一个null我并不真正想要的参数打开了大门。
更新节点后,我开始收到以下错误:
自定义文件输入:lang(en)~.custom-file-label -> 不匹配的伪类:lang
我不明白问题是什么。我该如何解决这个错误?我安装了节点 14.X。
我有一个简单的 .NET Core Web API 应用程序\xe2\x80\x94,这是创建新项目时由 Visual Studio 创建的应用程序。我想通过 FTP 将其部署到 Azure 应用服务,作为 Team Foundation Server (TFS) 2017 构建作业的一部分,这是成功的:
\n\n\n\n但是,当尝试发出GET如下 URL 之类的请求时:
http://somerandomname.azurewebsites.net/api/values\nRun Code Online (Sandbox Code Playgroud)\n\n我收到的只是一个 404,其中包含以下文字:
\n\n\n\n\n您正在查找的资源已被删除、更名或暂时不可用。
\n
从 Kudu 中,我收到以下错误:
\n\n\n\n我缺少什么?
\nI am trying to split up my ASP.NET Core 3.0 pre-release 9 MVC app into different projects. For example, I\xe2\x80\x99d like to have everything related to one subdomain go into a project called Website.SubdomainA and the other one into Website.SubdomainB.
我尝试使用 Razor 类库来完成此任务。根据MSDN,这些应该能够包含以下内容:
\n\n\n\n\nRazor 视图、页面、控制器、页面模型、Razor 组件、视图组件和数据模型可以构建到 Razor 类库 (RCL) 中。
\n
所以我继续创建了一个“RCL”,如下所示:
\n\n<Project Sdk="Microsoft.NET.Sdk.Razor">\n <PropertyGroup>\n <TargetFramework>netstandard2.1</TargetFramework>\n <RazorLangVersion>3.0</RazorLangVersion>\n </PropertyGroup>\n</Project>\nRun Code Online (Sandbox Code Playgroud)\n\n然后我复制了我的控制器和视图\xe2\x80\x94,但是由于某种原因,它找不到任何包含基类的程序集Controller,RouteAttribute以及包含在Microsoft.AspNetCore.Mvc namespace.
此外,我的视图无法编译,因为它们找不到所需的程序集,以及类似的东西Layout = "Test" get …
使用 ASP.NET Core 我收到以下令牌(简化):
String token = "Z%2F3+3Q==";
Run Code Online (Sandbox Code Playgroud)
的/是使用编码%2F。我从使用 Angular 在客户端上调用的 URL 收到了令牌。
我尝试使用以下方法解码令牌:
HttpUtility.UrlDecode(token)
Run Code Online (Sandbox Code Playgroud)
或者
WebUtility.UrlDecode(token)
Run Code Online (Sandbox Code Playgroud)
在这两种情况下%2F,都由/我想要的+代替,但由我不想要的空格代替。
我如何解码字符串?
更新
客户端应用程序正在发送编码的令牌:
Z%2F3%2B3Q%3D%3D;
Run Code Online (Sandbox Code Playgroud)
但是,不知何故,似乎令牌在以下操作中:
[HttpPut("account/verify/{userId}/{token}")]
public async Task<IActionResult> VerityEmailAddress([FromRoute]AccountEmailVerifyModel model) {
}
Run Code Online (Sandbox Code Playgroud)
转化为:
Z%2F3+3Q==
Run Code Online (Sandbox Code Playgroud)
所以+和=被解码,但/不是。
我有一个问题,我很困惑。我理解.NET的方式是这样的:
csc.exe (编译器)-> 如果是这样,为什么构建 .NET 应用程序的输出是 Windows 可执行文件?我认为整个想法是你的源代码编译成 MSIL,它针对本地安装的 CLR。但是,.NET Core 和 .NET 5 都会生成一个 Windows 可执行文件,它运行在操作系统之上,而不是在虚拟机中。
This is unlike Java, which compiles to a .class bytecode file (not an .exe), and those files do run atop a virtual machine (or, later, you can package all the class files into a .jar).
What I'm trying to ask is: Why doesn't the C# compiler generate a specific application file type that runs atop …
我的解决方案中有多个项目,都是 .NET Core 3.1。其中之一是我的核心项目(“ A Project ”),其中我只有基本的模型类,没有方法或数据库访问权限。出于演示目的,以下是 myAddress.cs和User.csfiles 的简化版本:
public class Address
{
public int Id {get;set;}
public string AddressText {get;set;}
public virtual User User {get;set;}
}
public class User
{
public int UserId {get;set;}
public int UserName {get;set;}
public ICollection<Address> {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
在另一个项目(“ B 项目”)中,我将构建实际的功能。该项目已经设置了带有ApplicationUser类的ASP.NET Core Identity,该类派生自IdentityUser并添加了一些自定义属性。
这就是我遇到问题的地方。该Address.User属性必须设置为 的实例ApplicationUser,但ApplicationUser位于B 项目中。
显然,我没有将 ASP.NET Core Identity 设置为我的A Project 中的依赖项,因此我无法将该ApplicationUser类移到该项目中。此外,我无法将 分配 …