我有一个webforms网站,正在调用我们正在开发的新MVC6网站.用户将使用表单身份验证在webforms网站上一直登录,然后重定向到新的MVC6网站.我知道在MVC6中我应该使用Cookie身份验证,但无法让它解密cookie.我怀疑它是关于web.config和machinekey的变化,但我真的被卡住了.
这就是我所做的.
我已按如下方式设置了cookie身份验证
app.UseCookieAuthentication(options =>
{
options.CookieName = "MyWebformsCookie";
options.AutomaticAuthenticate = true;
options.AuthenticationScheme = "Cookies";
options.TicketDataFormat = new MySecureDataFormat();
options.DataProtectionProvider = new MyDataProtectionProvider();
//options.CookieDomain = "localhost";
});
Run Code Online (Sandbox Code Playgroud)
课程如下
public class MySecureDataFormat : ISecureDataFormat<AuthenticationTicket>
{
public string Protect(AuthenticationTicket data)
{
return string.Empty;
}
public string Protect(AuthenticationTicket data, string purpose)
{
return string.Empty;
}
public AuthenticationTicket Unprotect(string protectedText)
{
return null;
}
public AuthenticationTicket Unprotect(string protectedText, string purpose)
{
var ticket = FormsAuthentication.Decrypt(protectedText);
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
正在读取cookie,并调用Unprotect方法,但随后在FormsAuthentication.Decrypt方法上出现错误并出现错误
System.Web.dll中出现"System.Web.HttpException"类型的异常,但未在用户代码中处理
其他信息:无法验证数据.
Stack = …
我无法让客户端验证在ASP.NET Core MVC中工作。我从这里Google搜索并应用了示例(http://www.discuzfeed.com/code/lotooslo-mvc-6-client-side-validation-for-custom-attribute.html),但是没有运气。
这是我的代码
属性
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
public sealed class CannotBeRedAttribute : ValidationAttribute, IClientModelValidator
{
public override bool IsValid(object value)
{
var message = value as string;
return false;
//return message?.ToLower() == "red";
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(
ClientModelValidationContext context)
{
yield return new ModelClientValidationRule(
"cannotbered",
FormatErrorMessage(ErrorMessage));
}
}
Run Code Online (Sandbox Code Playgroud)
查看模型
[CannotBeRed(ErrorMessage = "Not red!")]
public string SelectedCompanyIds { get; set; }
Run Code Online (Sandbox Code Playgroud)
视图
<input type="hidden" name="test" class="form-control" id="selectCompanyIdControl" data-bind="value: SelectedReportCompanyIds" asp- for="SelectedCompanyIds" />
@<script type="text/javascript"> …Run Code Online (Sandbox Code Playgroud) asp.net-core ×1
attributes ×1
c# ×1
client ×1
cookies ×1
encryption ×1
jquery ×1
validation ×1