检查数据库在处理之前是否返回记录的最有效方法是什么.例:Truck.where("id = ?", id).select('truck_no').first.truck_no
如果卡车存在,这可能会或可能不会返回卡车.在处理此请求时,确保页面不会崩溃的最有效方法是什么.如果让我说我使用循环来浏览每辆卡车并打印出它的编号,我将如何在视图和控制器中处理这个问题.
如果记录不存在,我希望能够打印出一条消息,而不是说没有找到记录.
我试图弄清楚如何注入UserManager和SignInManager.我在我的应用程序中安装了Ninject,并且我以下列方式使用它:
请将此视为一个全新的项目.在Startup.cs里面我有以下内容:
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
app.UseNinjectMiddleware(CreateKernel);
}
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
kernel.Load(Assembly.GetExecutingAssembly());
return kernel;
}
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我要创建一些Dummy类并尝试根据其工作的接口注入它.我测试了它.我想弄清楚的是,我现在将如何从Startup.Auth.cs中删除以下内容并将其注入.没有我可以依赖的接口,我不知道如何做到这一点:
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
Run Code Online (Sandbox Code Playgroud)
为了澄清一次,我的问题是:如何实例化ApplicationUserManager和ApplicationSignInManager并将其注入我的控制器参数中.这是我试图将其注入的控制器:
public AccountController(ApplicationUserManager userManager, ApplicationSignInManager signInManager)
{
UserManager = userManager;
SignInManager = signInManager;
}
Run Code Online (Sandbox Code Playgroud)
编辑:
这是我尝试过的:
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
kernel.Load(Assembly.GetExecutingAssembly());
kernel.Bind<IUserStore<ApplicationUser>>().To<UserStore<ApplicationUser>>();
kernel.Bind<UserManager<ApplicationUser>>().ToSelf();
return kernel;
}
Run Code Online (Sandbox Code Playgroud)
但有了这个我得到空引用错误
c# ninject entity-framework-6 asp.net-mvc-5 asp.net-identity
我对使用加密和rsa令牌还很陌生,我正在尝试让IDentityServer4不使用developersigning,而是我自己的一种。到目前为止,这是我尝试过的:
var keyInfo = new RSACryptoServiceProvider().ExportParameters(true);
var rsaSecurityKey = new RsaSecurityKey(new RSAParameters
{
D = keyInfo.D,
DP = keyInfo.DP,
DQ = keyInfo.DQ,
Exponent = keyInfo.Exponent,
InverseQ = keyInfo.InverseQ,
Modulus = keyInfo.Modulus,
P = keyInfo.P,
Q = keyInfo.Q
});
services.AddIdentityServer()
.AddSigningCredential(rsaSecurityKey)
.AddInMemoryPersistedGrants()
.AddInMemoryIdentityResources(Config.GetIdentityResources())
.AddInMemoryApiResources(Config.GetApiResources())
.AddInMemoryClients(Config.GetClients())
.AddAspNetIdentity<User>();
Run Code Online (Sandbox Code Playgroud)
但是,当我运行Identity Server4并重定向到另一个网站的登录页面时,出现以下错误:
IDX10630:用于签名的“ [PII被隐藏]”不能小于“ [PII被隐藏]”位。KeySize:“ [PII隐藏]”。参数名称:key.KeySize
我不得不承认,我已经呆了整整一个周末,试图弄清楚如何使用SigningCredentials,但我不确定我在上面做错了什么。
我试图以像素为单位检索浏览器的宽度但由于某种原因我回到了640,我的分辨率为1240.
我正在使用的代码是 Request.Browser.ScreenPixelsWidth
有没有人知道为什么它总是返回640或者是否有另一种方式让我在页面加载时获得浏览器的宽度?
我正在使用javascript/mobile-jquery接口为phonegap平台开发一个应用程序.现在我在网上看到了很多试图复制到剪贴板的例子,不幸的是它们都没有为我工作.我对浏览器中的功能不感兴趣,只要它通过手机间隙转换后就可以工作了,我很高兴.
我曾尝试使用zeroclipboard,它没有锻炼,我试过使用剪贴板管理器无法让它工作.我已经尝试了很多其他的例子,我在stackoverflow上找到了这些例子,包括google搜索,但它们仍然无法正常工作,这是我尝试过的一个例子:
window.plugins.clipboardManager.copy(
"the text to copy",
function(r){alert("copy is successful")},
function(e){alert(e)}
);
Run Code Online (Sandbox Code Playgroud)
我已经包含了js文件:
<script src="src/clipboardmanager.js"></script>
Run Code Online (Sandbox Code Playgroud)
我也有文件夹结构中的java文件,如下所示: src\com\saatcioglu\phonegap\clipboardmanager\ClipboardManagerPlugin.java
从我读过的内容来看,我需要包含一个xml文件才能正常工作,但就我而言,我无法在任何地方找到该XML文件.
任何帮助都非常感谢.
注意:我的应用程序不需要任何权限,如相机,GPS等...
编辑:
我试过的另一个例子是:
function select_all(obj) {
var text_val=eval(obj);
text_val.focus();
text_val.select();
if (!document.all) return; // IE only
r = text_val.createTextRange();
r.execCommand('copy');
}
Run Code Online (Sandbox Code Playgroud)
这适用于IE,但不适用于Phonegap.
编辑:
这是我正在使用的html/javascript:
<html>
<head>
<title>Test</title>
<link rel="stylesheet" href="jquery/jquery.mobile-1.3.1.min.css" />
<script src="jquery/jquery-1.9.1.min.js"></script>
<script src="jquery/jquery.mobile-1.3.1.min.js"></script>
<script src="clipboardmanager.js"></script>
<script>
var cbm = new window.plugins.clipboardManager;
function main(textMessage)
{
//Some Code before this (calculations)
cbm.copy(
"Success!!!",
function(r){alert("copy is successful")},
function(e){alert(e)}
);
}
</script>
</head> …
Run Code Online (Sandbox Code Playgroud) 我有以下客户:
new Client
{
ClientId = "nativeapptest",
ClientName = "Native App Test",
Enabled = true,
RequireClientSecret = false,
AllowedGrantTypes = GrantTypes.Code,
RedirectUris = { "com.mysite.nativeapp.12365789785256-buv2dwer7jjjjv5fckasdftn367psbrlb:/home" },
AllowedScopes =
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
"MyScope"
},
RequirePkce = false,
AllowOfflineAccess = true,
RequireConsent = false
}
Run Code Online (Sandbox Code Playgroud)
我使用本机脚本构建一个可以使用Identity Server 4登录的Android应用程序.目前发生的是我通过打开浏览器并使用所有正确的OpenID配置向IS4发出请求,最后我在登录屏幕上然后我选择使用Google登录.一旦进入谷歌,我输入我的电子邮件和密码,它一切都很好,然后谷歌试图将我送回我的网站,但它只是挂起...它的白页没有任何加载,它只是永远坐在那里,没有错误据我所知,is4记录的消息.
上面的nativescript登录部分来自OAutho2库https://www.npmjs.com/package/nativescript-oauth2
我试图理解这是IS4或原生Android应用程序的问题.页面是否挂起,因为它正在等待Android应用程序接管登录有效?Mabye问题在于RedirectURI计划?
它挂起的URL如下:
编辑:
由于我在实际的服务器上运行它,我无法直接调试它,但是,我确实添加了日志以查看代码的进度.我的日志告诉我用户已经通过谷歌和我的系统登录,我的日志也显示ExternalCallback已将页面重定向到
/connect/authorize/callback?client_id=nativeapptest&response_type=code&redirect_uri=com.mysite.nativeapp%3A%2F%2Fhome&scope=openid%20profile%20MyScope&response_mode=query&state=abcd
此时,页面挂起.
请注意,我们将RedirectUri更改为com.mysite.nativeapp以帮助进行测试.
最后,我不确定它是否重要,但我们没有使用https,因为这仍然是开发阶段.
如果Exception不为null,Nlog中是否存在输出特定字符的方法.例如我的布局是:
layout="${longdate}|${callsite:skipFrames=1}|${message}|${exception:format=tostring}"
Run Code Online (Sandbox Code Playgroud)
如果我调用NLog.Debug("Hello")
输出将是:
2015-12-07 11:50:00.5114|MyDll.MyClass.MyMethod|Hello|
Run Code Online (Sandbox Code Playgroud)
最后一个角色|
正在打印出来.有没有办法防止这种情况,只有在打印出实际异常时才打印出来?
我试图了解在使用EF6框架时使用一个DbContext类与多个对性能的影响.
例如,如果我们有一个简单的DbContext,例如:
public class MainDbContext : DbContext
{
public DbSet<Car> Cars { get; set; }
public void AddCar(Car car)
{
Cars.Add(car);
SaveChanges();
}
}
Run Code Online (Sandbox Code Playgroud)
现在让我们说我有一个使用上述DbContext的服务,方法如下:
public class CarService
{
public List<Car> Cars { get; private set; }
public CarService()
{
var dbContext = new MainDbContext();
Cars = dbContext.Cars.ToList();
}
}
Run Code Online (Sandbox Code Playgroud)
在什么时候DbContext进入数据库并检索存储在数据库中的所有汽车?是在我打电话的时候var dbContext = new MainDbContext();
还是在我打电话的时候Cars = dbContext.Cars.ToList();
?
如果是前者,如果我有一个包含100个表的DbContext,那么在创建DbContext时是否会查询所有100个表?
c# asp.net entity-framework entity-framework-6 asp.net-mvc-5
我试图弄清楚如何正确替换曾经是app.UseMvc()
.net core 2.2 的一部分的代码。这些示例甚至告诉我我可以调用的所有代码是什么,但我还不明白我应该调用哪个。例如,对于我的 MVC Web 应用程序,我有以下内容:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseStatusCodePagesWithReExecute("/Error/Index");
app.UseMiddleware<ExceptionHandler>();
app.UseStaticFiles(new StaticFileOptions()
{
OnPrepareResponse = (context) =>
{
context.Context.Response.GetTypedHeaders()
.CacheControl = new CacheControlHeaderValue
{
MaxAge = TimeSpan.FromDays(30)
};
}
});
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
});
}
Run Code Online (Sandbox Code Playgroud)
在我在UseMvc()
选项中提供我的路由之前。不过现在看来我必须在里面提供它MapControllerRoute
但是示例似乎总是也调用MapRazorPages()
. 我需要打电话给两个还是我想只打电话给一个?两者之间的实际区别是什么,如何设置默认控制器和默认操作?
我在.net core 2.1中有一个api构建。为了限制对各种端点的访问,我使用IdentityServer4和[Authorize]属性。但是,我在开发过程中的目标是向我们的开发人员公开api swagger文档,以便他们无论在哪里工作都可以使用它。我面临的挑战是如何保护swagger index.html文件,以便只有他们才能看到api的详细信息。
我已经在wwwroot / swagger / ui文件夹中创建了一个自定义index.html文件,并且一切正常,但是,该文件使用了来自/swagger/v1/swagger.json
不受保护的端点的数据。我想知道如何覆盖该特定端点的返回值,以便可以向其添加自己的身份验证?
编辑:
目前,我已经通过以下中间件实现了上述目标:
public class SwaggerInterceptor
{
private readonly RequestDelegate _next;
public SwaggerInterceptor(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
var uri = context.Request.Path.ToString();
if (uri.StartsWith("/swagger/ui/index.html"))
{
var param = context.Request.QueryString.Value;
if (!param.Equals("?key=123"))
{
context.Response.StatusCode = 404;
context.Response.ContentType = "application/json";
await context.Response.WriteAsync("{\"result:\" \"Not Found\"}", Encoding.UTF8);
return;
}
}
await _next.Invoke(context);
}
}
public class Startup
{
//omitted code
public void Configure(IApplicationBuilder app, IHostingEnvironment env) …
Run Code Online (Sandbox Code Playgroud) c# ×8
asp.net ×3
api ×1
cordova ×1
database ×1
javascript ×1
nativescript ×1
ninject ×1
nlog ×1
null ×1
routing ×1
rsa ×1
signing ×1
swagger ×1
swagger-ui ×1