我正在创建一个使用JWT进行身份验证和授权的Core 2.0 Web API项目.我想要保护的控制器方法都使用Authorize属性进行修饰.
这很有效.如果我在Bearer标题中传递JWT,我得到200.如果我没有通过JWT,我得到401.所有工作.在我的JWT中,我在授权时将用户ID存储在'UserId'字段中.
var claimsdata = new[] {
new Claim("UserId", user.Id.ToString()),
Run Code Online (Sandbox Code Playgroud)
然后我有一个扩展方法:
public static string GetUserId(this IPrincipal user)
{
if (user == null)
return string.Empty;
var identity = (ClaimsIdentity)user.Identity;
IEnumerable<Claim> claims = identity.Claims;
return claims.FirstOrDefault(s => s.Type == "UserId")?.Value;
}
Run Code Online (Sandbox Code Playgroud)
在我的控制器方法中,使用'Authorize',我经常需要用户的ID.所以我调用了我的GetUserId方法.这有效.但是,我不确定这是否是从令牌获取Id的最佳方式.
int.TryParse(User.GetUserId(), out _userId);
Run Code Online (Sandbox Code Playgroud)
我需要在所有控制器上使用该代码.我不能在构造函数中做到这一点,因为我认为这是错误的.
我在这做对了吗?
我正在尝试将 Serilog 依赖项注入到 .Net Core 3.1 中的 WebAPI 项目中的所有项目中。
在我的startup.cs中,我正在设置serilog,但可能是错误的:
我有:
using Serilog;
然后在: 中
public void ConfigureServices(IServiceCollection services),我有:
services.AddSingleton(Log.Logger);
我也有:
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.Development.json", optional: true, reloadOnChange: true)
.Build();
Log.Logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();
Run Code Online (Sandbox Code Playgroud)
在我的控制器类中,我做了通常的事情:
using Microsoft.Extensions.Logging;
private readonly ILogger _log;
public AuthorisationController(IConfiguration config, ISecurityService securityService, ILogger<AuthorisationController> log)
{
Configuration = config;
_securityService = securityService;
_log = log;
}
Run Code Online (Sandbox Code Playgroud)
Config 和 SecurityService 都正常。
但在我的第一种方法中,我尝试记录:
_log.LogInformation("Login attempt");
但我的日志中什么也没有出现。
任何人都可以发现错误吗?
这并不重要,但我的应用程序设置有这个:
"MinimumLevel": "Debug",
"WriteTo": [ …Run Code Online (Sandbox Code Playgroud) 我可以使用花哨的LINQ查询来返回a List<FileInfo>,方法是将它传递给方法(List<FileInfo> oldList, List<FileInfo> newList),并查看两个列表之间存在哪些差异?
基本上,我想获取添加到newList的任何文件的列表,这些文件在oldList中不可用.
我放弃了对Redux(我是React的新手)的希望,并且看到反过来的Alpha版本提供了一个新的Context.
所以我试图学习它,我的目标是,我有一个Navbar,我想在我的上下文中响应一个状态,{isAuthorised: false}.
我正在关注这些家伙的视频:
但他所有的代码都在一个文件中.我正在努力做到'正确'.
我所做的是创建了一个名为"context"的文件夹,并在其中创建了一个名为provider.jsx的jsx.
import React, {Component} from 'react';
export default class MyProvider extends Component {
constructor(props) {
super(props);
this.state = {
isAuthenticated: false
}
}
render() {
const MyContext = React.createContext();
return(
<MyContext.Provider value="Test Text">
{this.props.children}
</MyContext.Provider>
)
}
}
Run Code Online (Sandbox Code Playgroud)
https://github.com/CraigInBrisbane/ReactLearning/blob/master/src/context/provider.jsx
在那里,我在渲染中创建了上下文(这可能是错的......也许这意味着在我的App jsx中发生?).
我在那里创建了一个状态,默认是被认为是假的.(我稍后会添加代码以将其设置为应该是什么).
这编译...并运行.
在我的App组件中,我使用我的提供程序,如下所示:
import MyProvider from './context/provider.jsx';
export default class App extends Component {
render() {
return (
<MyProvider>
<div>
<Router>
<div>
<Navbar />
<Route exact path='/' component={Home} />
<Route …Run Code Online (Sandbox Code Playgroud) 我有一个List <>我的对象,但现在需要将其更改为IEnumerable <>.所以,我有这个:
public IEnumerable<TransactionSplitLine> TransactionSplitLines { get; set; }
Run Code Online (Sandbox Code Playgroud)
但是,我不能再做了:
reply.TransactionSplitLines.Add(new TransactionSplitLine
{Amount = "100", Category = "Test", SubCategory = "Test More", CategoryId=int.Parse(c)});
Run Code Online (Sandbox Code Playgroud)
我现在应该如何添加物品?
我需要根据对参数的Integer输入来按列排序结果集.
问题是,我需要为OrderBy使用CASE,似乎代码接受case列中的第一个'TYPE'...任何其他类型都失败.
我的代码是这样的:
WITH error_table AS
(
SELECT Row_Number() OVER
(ORDER BY
CASE @orderBy
WHEN 1 THEN received_date -- Last Rx'd message
WHEN 2 THEN message_id -- Message Id
WHEN 3 THEN zibmat.short_name -- Message action type
WHEN 4 THEN error_action.short_name -- Status type
WHEN 5 THEN ime.[allocated_date] -- Allocated Date
ELSE received_date
END) AS RowNumber
,ime.[ijis_message_error_id]
,ime.[message_id]
,ime.[message_version]
Run Code Online (Sandbox Code Playgroud)
因此,当OrderBy为1时,它可以工作.它按rx_date进行排序...但是当我发送它2时,它会因数据时间转换错误而失败.
看起来所有类型都必须相同......
发送5个工作正常,因为这也是一个约会时间.
有没有办法解决这个问题?
我们在生产中遇到了一些沉重的表锁定问题.我注意到我创建了一个存储过程,它按订单号获取订单列表.订单号是VARCHAR(150).此列上没有任何类型的索引.
目前,此列中有很多NULL值.但是,随着时间的推移(该表最近上线),该表将显着增长.此时不会再添加NULL值.
我的问题是双重的.首先,指数在这里是否有益.proc被大量使用.如果是这样,它应该聚集在一起吗?数据类似于CP123456,DR126512.
第二个问题,可能影响第一个问题是 - 将列更改为CHAR(10)是有益的,因为它似乎'订单号始终是相同的大小.将索引放在固定长度的CHAR上是否有任何速度优势,而不是VARCHAR(150)?
(大小不同是因为创建列时未知的要求).
SQL Server 2008.
我是Entity Framework的新手,但可能会误解某些内容,或者做错了什么.
我的代码,为我提供一个特定人员的任务列表:
public List<TaskObject> GetAssignedTasks(int personId)
{
var items = (from s in _te.tasks where s.person.person_id == personId select s).ToList();
var tasks = new List<TaskObject>();
foreach (var t in items)
{
TaskObject tk = Transformer.UnpackTask(t);
tasks.Add(tk);
}
return tasks;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,它似乎得到了一个记录列表,但没有加载相关的项目.我的'Transformer.UnpackTask'方法接受我加载的任务实体,然后将其转换为另一个对象,该对象通过业务/服务层进入UI.
但是一旦我的Unpacker函数尝试引用一个相关对象的项目(例如,一个任务有一个'AssignedPerson',它有一个带有人员详细信息的Person实体.但是我的实体的AssignedPerson属性是NULL.我想它会加载相关的项目.
我误会了吗?
在使用OWIN使用基于令牌的身份验证的在线教程之后,我设法让我的测试应用程序根据硬编码的用户名/密码进行身份验证,正如演示所做的那样.
但是,现在我希望使用我的Web应用程序中的模型.
正如演示所说,我的身份验证发生在这段代码中.
namespace UI
{
public class AuthorisationServerProvider : OAuthAuthorizationServerProvider
{
public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
{
context.Validated(); // Means I have validated the client.
}
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
// Here we validate the user...
var identity = new ClaimsIdentity(context.Options.AuthenticationType);
if (context.UserName == "user" && context.Password == "password")
{
identity.AddClaim(new Claim(ClaimTypes.Role, "admin"));
identity.AddClaim(new Claim("username", "user"));
identity.AddClaim(new Claim(ClaimTypes.Name, "My Full Name"));
context.Validated(identity);
}
else
{
context.SetError("Invalid grant", "Username or password are incorrect");
return;
}
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试学习 Context API,我想要实现的是在我的标题中显示登录用户,以及根据登录状态操作我的菜单选项(将“isAuthenticated”存储在状态?)
我的上下文类:
import React from 'react';
const Context = React.createContext();
export class Provider extends React.Component {
state = {
isAuthenticated: true,
user: {
name: "Joe Smith",
email: "joe.smith@here.com"
}
}
render() {
return (
<Context.Provider value={this.state}>
{this.props.children}
</Context.Provider>
)
}
}
export const Consumer = Context.Consumer;
Run Code Online (Sandbox Code Playgroud)
所以,非常基础。设置一个状态,以后在一些子组件中,我想显示chaps名称。
我的 App.js 是 using 'Provider',所以我的所有组件都可以访问这些数据:
import React from 'react';
import { HashRouter , Route, Switch } from 'react-router-dom';
import Home from './components/home';
import Header from './components/header';
import Accounts from './components/accounts';
import …Run Code Online (Sandbox Code Playgroud) c# ×6
.net ×2
asp.net-core ×2
javascript ×2
reactjs ×2
sql ×2
asp.net ×1
ienumerable ×1
jwt ×1
knockout.js ×1
linq ×1
list ×1
performance ×1
serilog ×1
sql-server ×1