请求我帮助实现向经过身份验证的用户分配声明的自定义方式.成功登录后,
var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
switch (result)
{
case SignInStatus.Success:
//Get the user
ApplicationUser user = UserManager.FindByEmail(model.Email);
//Ends here
ClaimsIdentity identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = true }, identity);
Run Code Online (Sandbox Code Playgroud)
我使用userId从数据存储区中获取有关用户的角色和其他信息.此后,我需要在重定向到用户仪表板之前,使用电子邮件,角色,名字,姓氏,性别等信息添加有关用户的声明.这是我尝试这样做的方式,但问题是即使在登录方法添加声明后,我也无法在_loginPartial剃刀视图中检索它
例如,当我想在登录部分显示电子邮件索赔值时,就像这样
var claims = ClaimsPrincipal.Current.Claims;
var principal = (ClaimsPrincipal)Thread.CurrentPrincipal;
var email = principal.Claims.Where(c => c.Type == ClaimTypes.Email).Select(c => c.Value).SingleOrDefault();
Run Code Online (Sandbox Code Playgroud)
它返回null.
因此,我只能在添加它们后使用相同的登录方法访问它们,但我需要能够从应用程序的任何位置访问它们.如果能够在整个申请中的任何其他地方检索这些声明,我将非常感谢.
谢谢.
我知道在 asp.net-core 2.2 中,我可以按如下方式获取操作名称:
ControllerContext.ActionDescriptor.ActionName
Run Code Online (Sandbox Code Playgroud)
但我正在寻找一种方法来获取 asp.net-core 2.2 中当前请求/页面的控制器名称
我将不胜感激任何指南。
谢谢
请我在这里需要帮助。我有一个表单要提交到另一个 url,但是当我尝试提交它时,它拒绝提交,我正在检查我的控制台。
在 Chrome 上,我看到以下错误
resources2.aspx?HCCID=75694719&culture=en-US&mlcv=3006&template=5:7 拒绝加载图像“ https://s4.mylivechat.com/livechat2/images/sprite.png ”,因为它违反了以下内容安全策略指令:“img-src'自我'数据:”。
拒绝将表单数据发送到“ https://cipg.stanbicibtcbank.com/MerchantServices/MakePayment.aspx ”,因为它违反了以下内容安全策略指令:“form-action 'self'”。
在 Mozilla Firefox 上,我看到以下内容:
内容安全策略:该页面的设置阻止资源的加载在https://s4.mylivechat.com/livechat2/images/sprite.png(“IMG-SRC http://smehelp.themarketplace.ng数据:”)
内容安全策略:该页面的设置阻止加载http://smehelp.themarketplace.ng/purchase/summary(“form-action 'self'”)上的资源。
在网上检查解决方案,我已将以下内容添加到我的页眉
<meta http-equiv="Content-Security-Policy" content="form-action 'self'">
Run Code Online (Sandbox Code Playgroud)
但问题仍然存在。
这导致我无法提交表单。早些时候,表单曾经被提交,但我今天刚刚尝试并观察到这个错误。
我在 MAC 操作系统上运行 Google Chrome 版本 55.0.2883.95(64 位)。
对于尽快解决此问题的任何建议,我将不胜感激。
谢谢
javascript forms jquery google-chrome content-security-policy
我正在尝试在 ASP.NET Core 5 项目中基于 ViewModel 构建视图,但发现很难通过以下阶段
我曾经能够通过删除数据上下文字段来通过这个阶段,但最近这似乎变得不可能了。关于如何在 ASP.Net Core 5 上使用 ViewModel 构建剃刀视图有什么想法吗?
对于 DataContext 字段,它会生成如图所示的错误
该模型不需要密钥。它是一个视图模型。它甚至不需要位于 AppDbContext 文件中,并且出于任何原因也不需要位于数据库中
visual-studio-2019 asp.net-core-scaffolding ef-core-5.0 asp.net-core-5.0
我试图在我的MVC应用程序中存储用户特定数据作为身份声明或会话数据之间做出选择,以减少数据库往返请求的数量和频率.但是,考虑到性能,安全性和其他最佳实践考虑因素,我不知道要走哪条路线.
我将对此提出任何建议表示感谢.
asp.net-mvc session-state claims-based-identity session-cookies cookie-session
我有 2 个表需要加入查询。第一个表是条目表,其中包含某些事件,例如舞蹈、说话、唱歌、播放等。
Id|Name
1|Dance
2|Sing
3|Speak
4|Play
5| etc.
Run Code Online (Sandbox Code Playgroud)
另一个表包含 userEntries,它指示每个用户在每个事件上的分数
Id| UserId|EntryId|Score
1|898128 | 1 |200
2|827329 | 2 |120
3|898128 | 2 |100
Run Code Online (Sandbox Code Playgroud)
现在我想要一个 linq 查询,首先获取所有条目,然后获取给定用户的分数,为条目重新调整 null 为用户有 noscore 的条目分数
用户 898128 的示例,我想看到这样的内容
Dance:200,Speak:null,Sing:120 从结果
我尝试了以下 linq 查询,但结果为空
var userScores =
(from e in db.Entries join se in db.UserEntries
on e.Id equals se.EntryId
into ese from se in
ese.DefaultIfEmpty()
where se.UserId == "898128"
select new
{
EntryLabel=e.Label,
EntryValue=se.ValueAmount,
}).ToList();
ViewData["userScores "] = userScores;
Run Code Online (Sandbox Code Playgroud)
我在 ASP.NET 核心 2.0 …
我一直试图让我的登录页面无法访问已登录的用户.除非他们没有登录,否则应该将他们重定向到他们的仪表板.
我做了以下工作,但他们都没有工作.有解决方案的人请?
// GET: /Account/Login
[AllowAnonymous]
public ActionResult Login(string returnUrl)
{
if (Request.IsAuthenticated)
{
RedirectToAction("Index", "Dashboard");
}
ViewBag.ReturnUrl = returnUrl ?? Url.Action("Index","Dashboard");
return View();
}
Run Code Online (Sandbox Code Playgroud)
这也是
// GET: /Account/Login
[AllowAnonymous]
public ActionResult Login(string returnUrl)
{
if (User.Identity.IsAuthenticated)
{
RedirectToAction("Index", "Dashboard");
}
ViewBag.ReturnUrl = returnUrl ?? Url.Action("Index","Dashboard");
return View();
}
Run Code Online (Sandbox Code Playgroud)
任何指南将不胜感激.谢谢
我有一个CodeIgniter应用程序仍在开发中,但我意识到当我登录并在会话中设置用户数据时,它会被设置但在重定向后消失.因此,重定向后无法访问会话数据.
这是我的登录脚本
public function login(){
if(!empty($_SESSION['user_id'])){//Meaning you are logged in
//We inform you
$this->session->set_flashdata('msg', "<div class='alert alert-success'><span class=''></span> You are already logged in as <strong>".$this->session->user_name."</strong></div>");
//And send you back to your dashboard
return redirect('/account/dashboard');
}
if($this->input->post()){
$this->load->library('form_validation');
$this->form_validation->set_rules('email','Email','required|valid_email');
$this->form_validation->set_rules('password','Password','required');
if($this->form_validation->run() == TRUE){
$creds=['email'=>$this->input->post('email'),'password'=>$this->input->post('password')];
$this->load->model('user/user_model');
$user=$this->user_model->login($creds);
if(!$user){
$data['error_msg']="Inavlid login details. Please retry";
}else{
$user->user_type=(!empty($user->user_type))?:'2';
$userdata=['user_id'=>$user->id,'user_name'=>$user->name,'user_email'=>$user->email,'user_type'=>$user->user_type];
$this->session->set_userdata($userdata);
$this->session->set_flashdata('msg', '<div class="alert alert-success"><span class="fa fa-check"></span> Logged in as '.$this->session->userdata('user_name').'</div>');
return redirect('account/dashboard');
}
}
}
$data['title']="Login";
$this->load->view('template/auth/header');
$this->load->view('account/login',$data);
$this->load->view('template/auth/footer');
}
Run Code Online (Sandbox Code Playgroud)
这是我的config/congig.php文件
$config['base_url'] = 'http://localhost/tmpad/';
$config['allow_get_array'] …Run Code Online (Sandbox Code Playgroud) 我有一个剃刀视图页面,我需要在尝试向用户显示文件之前检查文件是否存在,否则显示默认图像占位符。下面是我写的代码
@{
if (!string.IsNullOrEmpty(@parent.Photo) && File.Exists(Server.MapPath($"~/images/parents/thumbs/@parent.Photo")))
{
<img src="~/images/parents/thumbs/@parent.Photo" class="rounded-
circle img-fluid" />
}
else
{
@: No photo
}
}
Run Code Online (Sandbox Code Playgroud)
但我收到如下错误
当前上下文中不存在名称“服务器”
所以我无法构建应用程序。我试图在视图页面上添加以下 using 语句
using System;
using System.IO;
Run Code Online (Sandbox Code Playgroud)
但它没有区别
我的应用程序在 Windows 10 上的 ASP.NET-Core 3.1 上运行
asp.net-mvc ×3
c# ×3
asp.net ×2
codeigniter ×1
controller ×1
ef-core-5.0 ×1
forms ×1
httpcontext ×1
javascript ×1
jquery ×1
left-join ×1
linq ×1
php ×1
razor ×1
session ×1
sql-server ×1