如何在ASP.NET Core MVC中获取登录用户的角色?我想在用户登录应用程序后立即获取角色详细信息,但通过使用以下代码,我无法检索角色详细信息
public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null)
{
ViewData["ReturnUrl"] = returnUrl;
if (ModelState.IsValid)
{
var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure: false);
if (result.Succeeded)
{
_logger.LogInformation(1, "User logged in.");
bool available = User.IsInRole("Admin");
return RedirectToLocal(returnUrl);
}
if (result.RequiresTwoFactor)
{
return RedirectToAction(nameof(SendCode), new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
}
if (result.IsLockedOut)
{
_logger.LogWarning(2, "User account locked out.");
return View("Lockout");
}
else
{
ModelState.AddModelError(string.Empty, "Invalid login attempt.");
return View(model);
}
}
// If …
Run Code Online (Sandbox Code Playgroud) 我有以下表名JobTitle
JobID LanaguageID
-----------------
1 1
1 2
1 3
2 1
2 2
3 4
4 5
5 2
Run Code Online (Sandbox Code Playgroud)
我从表中选择所有记录,除了重复JobID的计数> 1.我只从重复的JobID中选择一个记录/第一行.现在我将LanguageID作为参数传递给存储过程,我想为该languageID选择重复的JobID以及其他记录.如果我已将languageID作为1传递,则输出应如下所示
JobID LanaguageID
-----------------
1 1
2 1
3 4
4 5
5 2
Run Code Online (Sandbox Code Playgroud)
我尝试过使用以下查询.
with CTE_RN as
(
SELECT ROW_NUMBER() OVER(PARTITION BY JobTitle.JobID ORDER BY JobTitle.JobTitle) AS RN
FROM JobTitle
INNER JOIN JobTitle_Lang
ON JobTitle.JobTitleID = JobTitle_Lang.JobTitleID
)
Run Code Online (Sandbox Code Playgroud)
但我无法在上面的查询中使用WHERE子句.是否应遵循任何不同的方法.或者我如何修改查询以获得所需的输出
在使用存储过程检索结果时,如何在.net核心的视图模型中检索和存储多个结果集
例如,从存储过程我返回两个以下查询的记录
Select * LMS_Survey
Select * from LMS_SurveyQuestion
Select * from LMS_SurveyQuestionOptionChoice
Run Code Online (Sandbox Code Playgroud)
以下是两个表的视图模型
public class LMS_SurveyTraineeViewModel
{
public LMS_SurveyDetailsViewModel SurveyDetailsViewModel { get; set; }
public LMS_SurveyQuestionsViewModel SurveyQuestionsViewModel { get; set; }
public LMS_SurveyQuestionOptionChoiceViewModel SurveyQuestionOptionChoiceViewModel { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这就是我执行存储过程的方式
public List<LMS_SurveyTraineeViewModel> GetTraineeSurvey(int surveyID)
{
try
{
List<LMS_SurveyTraineeViewModel> modelList = new List<LMS_SurveyTraineeViewModel>();
modelList = dbcontext.Set<LMS_SurveyTraineeViewModel>().FromSql("LMSSP_GetTraineeSurvey @surveyID = {0},@LanguageID = {1}", surveyID, AppTenant.SelectedLanguageID).ToList();
return modelList;
}
catch (Exception ex)
{
throw ex;
}
}
Run Code Online (Sandbox Code Playgroud)
如何在视图模型中使用存储过程存储多个结果集?
我的.net核心应用程序中出现以下错误
错误-4077 ECONNRESET连接由对等方重置
这究竟意味着什么?
我注意到如果长时间保持我的应用程序处于空闲状态,如1或2小时,则会发生此错误,
此外,我已经在我的应用程序中处理了会话超时,这对于给定的规定会话超时效果很好,可以说30分钟,但如果超过这个很长时间,那么我得到上述错误.
并且,我没有在我的应用程序中使用任何套接字.
对此有任何帮助表示赞赏!
如何在不使用 api 密钥的情况下获取 youtube 视频标题和持续时间?
我在没有 API 密钥的情况下使用 API v3检查了Youtube 视频标题?链接但这只给出标题而不是持续时间。
那么我怎样才能在没有 api 密钥的情况下获得持续时间呢?
我的中间件类在不同的类库项目中,控制器在不同的项目中.我想要做的是,如果特定条件不符合,那么从中间件重定向到自定义控制器/操作方法.
但是,我无法使用Response.Redirect方法执行此操作.
我怎样才能在中间件类中做到这一点?
对此有任何帮助表示赞赏!
罗希特
我的问题是如何在以前的.net版本中编写的.net核心中编写Windows服务?
很多链接/文章解释了如何将.net核心应用程序托管为Windows服务.那么这是我创建Windows服务的唯一方法吗?
如果是的话,任何人都可以提供它的链接/示例
谢谢 !
We are trying to redirect the user(using return URL) to the login page if the user is not authenticated/authorized while accessing the particular URL. However, we are not able to add the custom parameters(clientname in this case) in route while redirecting the user to the login page. We are using asp.net identity core framework.
In Startup.cs we have defined the below route which will be applicable to all.
app.UseMvc(routes =>
{
routes.MapRoute(
name: "Edge",
template: "{clientname}/{controller}/{action}");
});
Run Code Online (Sandbox Code Playgroud)
also added below …
c# asp.net-mvc asp.net-identity asp.net-core-mvc asp.net-core
我试图通过在 .net core 项目中的类中获取配置来注入依赖项。我尝试注入依赖项的类位于另一个项目中。但不知何故,我无法从注入的依赖项中的配置文件中获取值。下面是我的代码
在下面的 DBContext 中,我需要从配置中获取值,其中我使用了 DBConfiguration 类的 DI。
public class DBContext : DbContext
{
private readonly DBConfiguration _dBConfiguration;
public DBContext(DBConfiguration dBConfiguration)
{
_dBConfiguration = dBConfiguration;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(_dBConfiguration.ConnectionString);
base.OnConfiguring(optionsBuilder);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
Run Code Online (Sandbox Code Playgroud)
以及我的 Web api 中的 StartUp.cs 文件
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<DBConfiguration>();
services.AddEntityFrameworkSqlServer().AddDbContext<DBContext>();
services.AddOptions();
services.Configure<DBConfiguration>(Configuration.GetSection("DBConfiguration"));
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
Run Code Online (Sandbox Code Playgroud)
和我的 appsettings.json 文件
{
"DBConfiguration": {
"ConnectionString": "Server=myserver;Database=BaseProjectDB;Trusted_Connection=True;MultipleActiveResultSets=true",
"ApplicationName": "WebAPI"
},
"Logging": {
"LogLevel": {
"Default": "Warning" …
Run Code Online (Sandbox Code Playgroud) 以前有些类存在于MyProject.Web.Core
项目中,它们被移动到MyProject.Model
项目中.但是,这些移动的类具有名称空间MyProject.Web.Core
.
那么,我如何更改所有类的命名空间,MyProject.Model
因为我们不想手动更新它们.VS2017有没有可用的选项?
我们已经更新了项目属性中的默认命名空间,因此在添加新类时将应用它.但是,移动/旧课程怎么样?
c# ×8
asp.net-core ×7
.net-core ×2
asp.net-mvc ×1
middleware ×1
namespaces ×1
refactoring ×1
sql ×1
sql-server ×1
user-roles ×1
youtube ×1
youtube-api ×1