在以前版本的MVC框架中,将通过实现IClientValidatable和GetClientValidationRules方法来实现自定义验证.
但是在ASP.Net Core MVC中我们没有这个接口,尽管我们确实有IClientModelValidator一个定义非常相似的方法.然而,其实现永远不会被调用.
那么 - 我们如何在ASP.NET Core MVC中为自定义属性实现客户端验证?
我正在研究在ASP.NET(MVC Core 1.0)中使用基于声明的授权.设置a时ClaimsIdentity,我提供了一个键/值字符串对列表来表示每个Claim.例:
List<Claim> claims = new List<Claim>
{
new Claim("UserID", user.ID),
new Claim("Name", user.Name),
new Claim("Role", "basic")
};
Run Code Online (Sandbox Code Playgroud)
我的理解是我可以使用我想要的任何键/值.但是我注意到ClaimsType课程中有一些预定义的键可用.所以,我可能会使用其中一些预定义的键:
List<Claim> claims = new List<Claim>
{
new Claim(ClaimTypes.Sid, user.ID),
new Claim(ClaimTypes.Name, user.Name),
new Claim(ClaimTypes.Role, "basic")
};
Run Code Online (Sandbox Code Playgroud)
问题:
如果我使用预定义的密钥,是否有关于分配给每个密钥的实际值的规则/限制,还是应用程序定义的?例如,是否可以粘贴数据库主键ClaimTypes.Sid,或者ASP.NET是否对ClaimTypes.Sid应该包含的内容有一定的期望?
是否有任何ClaimTypes所需要的,或者是完全由应用程序来决定什么包括或不包括什么呢?我想,答案可能取决于我将与之交互的特定第三方身份验证服务,但是如何使用不包含任何第三方身份验证的自包含ASP.NET项目的简单情况.ASP.NET本身是否有任何要求?
任何与使用特定键/值的要求和/或最佳实践的链接都将受到赞赏.
下面的Swift 2示例给出了以下错误:
String类型的值没有成员'stringByAppendingPathComponent'

我需要为Swift 3更改什么?
考虑以下简单程序(使用Visual Studio 2015):
public class Program
{
public static void Main(string[] args)
{
var dtClass = new MyDateTimeWrapperClass(DateTime.Today);
var dtStruct = new MyDateTimeWrapperStruct(DateTime.Today);
WriteLine(dtClass);
WriteLine(dtStruct);
ReadKey();
}
}
public class MyDateTimeWrapperClass
{
private readonly DateTime _value;
public MyDateTimeWrapperClass(DateTime value)
{
_value = value;
}
public override string ToString() => _value.ToString("MM/dd/yyyy");
}
public struct MyDateTimeWrapperStruct
{
private readonly DateTime _value;
public MyDateTimeWrapperStruct(DateTime value)
{
_value = value;
}
public override string ToString() => _value.ToString("MM/dd/yyyy");
}
Run Code Online (Sandbox Code Playgroud)
控制台将报告方法的预期输出ToString.但是,在调试模式下,输出不一样.
我一直以为Visual Studio使用 …
我在VS 2015中创建了一个ASP.NET核心Web应用程序.添加单元测试复选框可用于ASP.NET 4.x模板,但是当我选择ASP.NET 5模板时,它显示为灰色.无论如何我试图创建项目,计划添加我自己的单元测试.
我在解决方案中添加了另一个项目,但我无法将这两个项目联系起来.当我右键单击UnitTest项目的引用并点击Add Reference时,我可以看到原始项目 作为选项.
但是当我选中方框并点击OK时,我会看到一个对话框,上面写着"无法添加对'PangolinWeb'的引用.程序集必须有'dll'或'exe'扩展才能被引用."
为什么我不能添加ASP.NET Core项目作为参考?这是使我的UnitTest项目可以使用它的所有类和方法的唯一方法吗?
我在IIS8下部署了两个ASP.NET MVC Web应用程序(让我称之为发送方和接收方Web应用程序).我从发送方内部的操作方法调用接收方Web应用程序中的操作方法.
现在在发件人中,我有以下操作方法,将上传string到接收器上的外部操作方法:
using (WebClient wc = new WebClient())
{
var data = JsonConvert.SerializeObject(resource);
string url = "https://receiver/CreateResource?AUTHTOKEN=" + pmtoken;
Uri uri = new Uri(url);
wc.Encoding = System.Text.Encoding.UTF8;
wc.Headers.Add(HttpRequestHeader.ContentType, "application/json; charset=utf-8");
wc.Headers.Add("Authorization", token);
output = wc.UploadString(uri, data)
}
Run Code Online (Sandbox Code Playgroud)
我编码string使用UTF-8在上传之前,因为我将传递Unicode字符,如£,¬等...
在接收方Web应用程序上,接收操作方法如下:
public List<CRUDOutput> CreateResource(Resource resourceinfo)
Run Code Online (Sandbox Code Playgroud)
一开始我认为我的方法不会很好.因为我正在wc.Encoding = System.Text.Encoding.UTF8;从发送器向接收器动作方法发送编码数据(使用),并且我没有对接收器动作方法进行任何类型的解码.
但是,似乎在接收动作方法上resourceinfo接收到正确的解码值.所以看起来ASP.NET MVC会在某处自动处理解码.
第一个问题:
谁能告诉我ASP.NET MVC如何处理接收操作方法中的解码?
第二个问题:
在我的WebClient()方法中,我定义以下内容以指定内容类型标头:
wc.Headers.Add(HttpRequestHeader.ContentType, "application/json; charset=utf-8");
Run Code Online (Sandbox Code Playgroud)
然而,似乎这对我的情况没有任何影响.当我删除上面的代码行时,没有任何变化.任何人都可以告诉我,如果定义内容类型标题会对我的情况有任何影响吗?
最后一个问题:
如果我没有明确定义UTF-8编码使用wc.Encoding = System.Text.Encoding.UTF8;将WebClient() …
我有一个下拉列表,我想将 Jquery onchange 事件绑定到 TagHelpers 选择标记。下面是我的代码。
<select asp-for="BusinessType"
asp-items="@Model.BusinessTypeCollection">
</select>
Run Code Online (Sandbox Code Playgroud)
如何绑定标记的内联 onchange 事件。
像这样的东西。
<select asp-for="BusinessType"
asp-items="@Model.BusinessTypeCollection"
onchange ="something">
</select>
Run Code Online (Sandbox Code Playgroud) ASP.Net Core noob here ...我正在使用DNX的Net.Net核心WebAPI核心项目.
我需要在我们的服务中实现API Key auth.为此,我创建了中间件,从请求中获取信息并继续进行身份验证.支持转到数据库,获取匹配的密钥,然后返回并进行验证.
这是为了查看上下文并获取APIKey而实现的中间件
的AuthenticationHandler
public class AuthorizationHandler
{
private readonly RequestDelegate _next;
private IAuthenticationService _authenticationService;
public AuthorizationHandler(RequestDelegate next, IAuthenticationService authService)
{
_authenticationService = authService;
_next = next;
}
public async Task Invoke(HttpContext context)
{
try
{
var apiKey = context.Request.Headers["Key"];
var location = context.Request.Headers["Host"];
var locationKey = _authenticationService.GetApiKey(location);
if (apiKey == locationKey)
await _next(context);
context.Response.StatusCode = 403;
context.Response.Headers.Add("WWW-Authenticate",
new[] { "Basic" });
}
catch (Exception ex)
{
context.Response.StatusCode = 500;
context.Response.Headers.Add("WWW-Authenticate",
new[] { "Basic" …Run Code Online (Sandbox Code Playgroud) 我正在使用visual studio 2015并使用mvc core创建了一个空白的mvc应用程序.
在startup.cs文件中,我添加了一个新的测试方法.然后我将它附加到hello world字符串.如果我只是运行它,一切正常.我得到了"Hello World!3".但如果我尝试调试我的代码.我在测试方法中设置了几个断点.当我移动鼠标移动器那些a,b,c变量时.我一直得到":错误CS0103:当前上下文中不存在名称'xxxx'".这发生在今天.我创建了这个全新的应用程序,除了这个简单的方法之外什么都没有.
这是我到目前为止所做的.重启visual studio,重置所有设置.但仍然得到相同的错误消息.我认为它是视觉工作室.
public class Startup
{
// 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 http://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app)
{
int d = test();
app.UseIISPlatformHandler();
app.Run(async (context) =>
{
await …Run Code Online (Sandbox Code Playgroud) Retrofit retrofit = new Retrofit.Builder().baseUrl(Api.Q_BASE)
.addConverterFactory(GsonConverterFactory.create()).client(client).build();
Api.ApiInter inter = retrofit.create(Api.ApiInter.class);
Call<JSONObject> call = inter.getMovieData(sortType[0], Api.P_KEY);
call.enqueue(new Callback<JSONObject>() {
@Override
public void onResponse(Call<JSONObject> call, Response<JSONObject> response) {
/* */
}
@Override
public void onFailure(Call<JSONObject> call, Throwable t) {
Log.d(LOG_TAG, "failed to get response.");
}
});
Run Code Online (Sandbox Code Playgroud)
/
public class Api {
public static final String Q_BASE = "http://api.themoviedb.org/";
public static final String Q_KEY = "api_key";
public static final String Q_RESULT = "results";
public static final String Q_POSTER_PATH = "poster_path";
public static final …Run Code Online (Sandbox Code Playgroud) 我正在使用自定义验证属性并希望显示我的验证消息,我将在其中指定一个名称,如下所示:
return new ValidationResult(FormatErrorMessage("Hobbies"));
Run Code Online (Sandbox Code Playgroud)
我发现这个 html 在页面上:
<span data-valmsg-replace="true" data-valmsg-for="Hobbies" class="field-validation-valid text-danger"></span>
Run Code Online (Sandbox Code Playgroud)
当我像这样从类的IsValid函数返回消息时ValidationAttribute:
return new ValidationResult(FormatErrorMessage("Hobbies"));
Run Code Online (Sandbox Code Playgroud)
在我指定名称“爱好”的地方,应该将其添加到
<span data-valmsg-for="Hobbies">
Run Code Online (Sandbox Code Playgroud)
为什么没有添加我的错误信息?
如果你想让我粘贴完整的代码,请告诉我。
asp.net-core ×4
c# ×4
asp.net-mvc ×3
.net ×2
asp.net ×2
validation ×2
android ×1
json ×1
retrofit2 ×1
swift3 ×1
unit-testing ×1
webclient ×1