我在 ASP.NET Core Web API 上有一个后端,在 Angular 9 上有一个前端。我正在实施谷歌身份验证。在邮递员中它工作正常,但是在我的 angular 应用程序中,当我尝试通过 google 进行授权时,出现错误:
Access to XMLHttpRequest at 'https://accounts.google.com/o/oauth2/v2/...
(redirected from 'https://localhost:44340/api/auth/googlelogin')
from origin 'http://localhost:4200' has been blocked by CORS policy:
Response to preflight request doesn't pass access control check:
Redirect is not allowed for a preflight request.
Run Code Online (Sandbox Code Playgroud)
后端的 CORS 配置:
services.AddCors(options =>
{
options.AddPolicy("MyPolicy",
builder =>
{
builder
.WithOrigins(Configuration["ApplicationSettings:Client_URL"].ToString())
.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod();
});
});
Run Code Online (Sandbox Code Playgroud)
app.UseCors("MyPolicy");
Run Code Online (Sandbox Code Playgroud)
我的服务中的方法
public ChallengeResult LoginViaGoogle()
{
var provider = "Google";
var redirectUrl = "/api/auth/ExternalLoginCallBack";
var properties …Run Code Online (Sandbox Code Playgroud) 我有 2 个字符串列表
List<string> A = new List<string> { "1QWERTY", "ASDF4B2J", "C0QW", "8D" };
List<string> B = new List<string> { "GJ65SJ2", "KO21L94B", "ABF287KF2", "GPK25" };
Run Code Online (Sandbox Code Playgroud)
我想获得相同长度的字符串的第一个和第二个字母对(通过使用 LINQ)。
喜欢1Q: GJ或AS: KO。
所以,我写了这段代码:
var res = A.Join(
B,
a => a.Length,
b => b.Length,
(a, b) => new { str = a[0] + a[1] + ": " + b[0] + b[1] }
);
foreach (var e in res)
{
Console.WriteLine(e.str);
}
Run Code Online (Sandbox Code Playgroud)
但是,而不是使用1Q或AS使用a[0], …
我想知道如何在 TypeScript 中重新创建此 C# 代码
Dictionary<string, List<string>> sas = new Dictionary<string, List<string>>();
sas.Add("11", new List<string> { "a", "b", "c" });
sas.Add("22", new List<string> { "a2", "b2", "c2" });
foreach(var e in sas)
{
Console.WriteLine(e.Key);
foreach(var s in e.Value)
{
Console.WriteLine(s);
}
Console.WriteLine();
}
Run Code Online (Sandbox Code Playgroud)
我希望我会进入控制台:
11
a
b
c
22
a2
b2
c2
Run Code Online (Sandbox Code Playgroud)
更新
最重要的是,我如何在 Angular 中使用这样的代码以及为什么我的 ngOnInit 方法未定义?
我的回答课
import { Question } from './question';
export class Answer {
AnswerId: number;
Content: string;
IsCorrect: boolean;
Mark: number;
QuestionId: number;
Question: …Run Code Online (Sandbox Code Playgroud) 我一直在观看 Jason Taylor 关于使用 ASP.NET Core 进行清洁架构的演示(时间 35:52)
https://www.youtube.com/watch?v=dK4Yb6-LxAk&t=2134s&ab_channel=GOTOConferences
他创建了一个DateTimeService返回DateTime.Now并通过AddTransient方法注册的方法,他建议使用它而不是DateTime.Now直接使用,因为在这种情况下他不依赖于机器时钟。
我不清楚为什么他在这种情况下没有依赖性,你能解释一下它是如何工作的吗?
这是用于销售火车票的未完成的数据库。
我想在路由表中为 RouteId 创建主键,但出现异常:
CREATE UNIQUE INDEX 语句终止,因为找到了对象名称“dbo.Route”和索引名称“PK_Route”的重复键。重复的键值为 (1)。
但是没有另一个键。
我认为问题可能是最初我有 2 个表 Route 和 RouteStation,然后我删除了表 Route 并将 RouteStation 重命名为 Route。
本网站上的另一个主题对我没有帮助。我还尝试查看此表的键,但输出为空:
SELECT Col.Column_Name from
INFORMATION_SCHEMA.TABLE_CONSTRAINTS Tab,
INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE Col
WHERE
Col.Constraint_Name = Tab.Constraint_Name
AND Col.Table_Name = Tab.Table_Name
AND Constraint_Type = 'PRIMARY KEY'
AND Col.Table_Name = 'Route'
Run Code Online (Sandbox Code Playgroud) 输入一个单词,然后需要用相同数量的符号创建一个新单词,并且新单词必须仅用“ _”填充。
int main()
{
char word[30];
cout << "Enter word: ";
gets_s(word);
cout << word;
int k = strlen(word);
cout << "Amount of letters in word: "<< k << endl;
char *temp = new char[k];
for (int i = 0; i < k; i++)
{
temp[i] = '_';
}
cout << temp << endl;
}
Run Code Online (Sandbox Code Playgroud) c# ×4
asp.net-core ×2
.net ×1
.net-core ×1
angular ×1
c ×1
cors ×1
datetime ×1
duplicates ×1
google-api ×1
google-oauth ×1
linq ×1
list ×1
sql ×1
sql-server ×1
string ×1
typescript ×1