我已经开始使用新的 ASP.Net 身份,我想知道身份如何处理 IsOnline ...?它有这个选项吗?
另一个使用 mvc 5 和身份的问题,我如何最好地跟踪在线 ppl?当用户关闭页面然后 js 卸载以将 db 中的属性设置为 IsOnline 为 false 时,我是否仍应使用旧方法?
但是,如果用户打开了网站的 2 个甚至 3 个实例并浏览了不同的页面,我该如何正确并同步维护在线/离线?
也许有某种图书馆可以做到这一点,或者有一些新的方法可以做到这一点?也许一些有用的链接....
谢谢
我有一个使用实体框架和存储库模式的 MVC 5 应用程序。我有一个用于用户表 (AspNetUsers) 的存储库和一个名为 GetAll() 的存储库中的方法,用于从该表中获取所有用户。
我需要向我的应用程序的用户显示所有用户及其角色,但我很难通过我的用户存储库获取角色的实际名称。我正确编写的 GetAll() 方法将所有用户及其角色 ID 返回给我,如关联表 AspNetUserRoles 中所定义,但我还需要加载角色名称。
从我的存储库中获取所有用户的代码行如下所示:
var users = UsersRepository.GetAll();
Run Code Online (Sandbox Code Playgroud)
我想要的是获取分配给用户的角色的角色名称,以便我可以在应用程序中显示。
对于那些不熟悉 ASP.NET Identity 表的人,有一个表包含看起来像这样的用户信息(为简洁起见,仅列出了相关字段):
AspNet用户
然后还有另一个表,其中包含看起来像这样的角色信息(为简洁起见,仅列出了相关字段):
AspNet角色
最后,中间有一个关联表,看起来像这样:
AspNet 用户角色
所以我的基本目标是从用户对象通过关联表来获取我的存储库查询中的所有角色名称。我在想这样的事情:
var usersAndRoles = UsersRepository.GetAll().Include(.......)
Run Code Online (Sandbox Code Playgroud) c# asp.net entity-framework repository-pattern asp.net-identity
好吧,我不知所措。我到处找,但仍然有错误。我有一个文件夹,里面存储了几个 pdf 文件。该文件夹名为“docs”,位于我项目的根目录中。我使用以下代码在文件夹中放置了一个 web.config 文件...
<system.web>
<authorization>
<deny users="?" />
</authorization>
</system.web>
Run Code Online (Sandbox Code Playgroud)
我还尝试使用以下代码将代码放在我的根 web.config 文件中...
<location path="/docs">
<system.web>
<authorization>
<deny users="?" />
</authorization>
</system.web>
</location>
Run Code Online (Sandbox Code Playgroud)
这两个代码块都会产生 500 服务器错误。不幸的是,我无法访问详细的服务器错误,因为我在共享主机上。有任何想法吗?
编辑:对不起......这就是我在凌晨 1 点发布问题所得到的。我想保护该文件夹,以便只有登录并获得授权的用户才能访问它并下载文件。
我正在拼凑来自博客和不同地方的代码片段,试图让它发挥作用。通常,我会参考参考文档,但在这里或其他任何地方都找不到。它只是针对特定用例的视频和演示,包括用户管理或 Facebook 或 Twitter。
我有一个我正在使用的专有身份验证服务。用户帐户不在我的应用程序中管理。所以我需要能够登录一个完全在运行时构建的用户。
这是我现在在我的 MVC 应用程序中尝试的内容。
using System.Security.Claims;
public class HomeController : Controller {
public ActionResult Scratch() {
var claims = new Claim[] {
new Claim(ClaimTypes.Name, "somename"),
new Claim(ClaimTypes.NameIdentifier, "someidentifier"),
new Claim("foo", "bar"),
};
var identity = new ClaimsIdentity(claims);
var authenticationManager = HttpContext.GetOwinContext().Authentication;
authenticationManager.SignIn(identity);
return Content(
$"authentication manager type: {authenticationManager.GetType()} \n"
+ $"authenticated: {HttpContext.User.Identity.IsAuthenticated} \n"
+ $"user name: {HttpContext.User.Identity.Name} \n",
"text/plain");
}
}
Run Code Online (Sandbox Code Playgroud)
输出是
authentication manager type: Microsoft.Owin.Security.AuthenticationManager
authenticated: False
user name:
Run Code Online (Sandbox Code Playgroud)
问题:
我使用代码优先的方法。我们总是不喜欢在我们的表中添加 NVarchar(max)。但是当我们默认使用身份帐户管理时,AspNetUser 表中的列包含 NVarchar(max),如下图所示。我的团队认为使用 varchar(Max) 会降低性能。
在数据库中
在代码中
现在我的问题是
1) 是否有必要在该列中使用 varchar(max)?
2)我们可以减少该列的大小吗?
3)如果是,如何减少该列的大小?
4) 当使用 Varchar(Max) 时,真的会出现这样的性能问题吗?(我知道为什么我们不应该使用它而是指导我,这是一个大问题吗)
许多文章只解释了如何在该表中包含新列,而不是修改现有列。请帮助我。
对不起,如果你不明白我的问题,请问我。
我正在使用 Asp.net Identity 框架进行身份验证。现在我需要来自 connectionId 或已连接用户角色的已连接客户端的用户 ID。
使用 Identity Framework 创建了一个 ASP.Net 应用程序。用户认证成功后,System.Web.HttpContext.Current.User.Identity是null和 System.Web.HttpContext.Current.User.Identity.IsAuthenticated是false
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (!ModelState.IsValid)
{
return View(model);
}
var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: true);
switch (result)
{
case SignInStatus.Success:
ApplicationUser user = System.Web.HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>().FindById(System.Web.HttpContext.Current.User.Identity.GetUserId());
UserManager.AddClaim(User.Identity.GetUserId(),new Claim(ClaimTypes.Role,"Admin"));
Run Code Online (Sandbox Code Playgroud)
我需要在身份验证后向经过身份验证的用户添加一些角色。
我使用此代码检查登录:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (!ModelState.IsValid)
{
return View(model);
}
var user = await UserManager.FindByEmailAsync(model.Email);
if (user != null)
{
if (!await UserManager.IsEmailConfirmedAsync(user.Id))
{
ViewBag.errorMessage = "You must have a confirmed email to log on.";
return View("Error");
}
}
var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
switch (result)
{
case SignInStatus.Success:
return RedirectToLocal(returnUrl);
case SignInStatus.LockedOut:
return View("Lockout");
case SignInStatus.RequiresVerification:
return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe }); …Run Code Online (Sandbox Code Playgroud) 我有一个 .net core 2.0 应用程序,正在实施外部登录提供程序,如 google、twitter 和 facebook。我需要获取用户的显示名称和个人资料图片,但在 .net core 2.0 中找不到任何有关如何实现此目的的文档。
我像这篇文章一样添加身份验证:https : //docs.microsoft.com/en-us/aspnet/core/security/authentication/social/
这是我的推特登录和回调函数...
[HttpGet]
[Route("/api/security/login/type/socialmedia/twitter")]
public IActionResult GetTwitterLogin(string redirect_uri)
{
ClientCallback = redirect_uri;
string redirectUrl = "/api/security/login/type/socialmedia/twittercallback";
var properties = SignInManager.ConfigureExternalAuthenticationProperties("Twitter", redirectUrl);
return Challenge(properties, "Twitter");
}
[HttpGet]
[Route("/api/security/login/type/socialmedia/twittercallback")]
public async Task<HttpResponseMessage> GetTwitterCallBackAsync()
{
var info = await SignInManager.GetExternalLoginInfoAsync();
var result = await SignInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent: false, bypassTwoFactor: true);
if (result.Succeeded)
{
}
else
{
}
Response.StatusCode = (int)HttpStatusCode.OK;
return null;
}
Run Code Online (Sandbox Code Playgroud)
看起来您可以从 info.Principal.Claims 获取一些项目,但对于用户的显示名称或个人资料图片却没有。
您如何获得各种登录提供商的显示名称或个人资料图片?
我有以下简单的代码使用该CreateAsync方法创建新用户。该代码不会引发任何错误,但是也不会更新数据库。我在该IdentityResult result行的断点处添加了一个断点,并在if语句中的断点处添加了一个断点。结果,我不知道如何调试此代码并查找错误。有什么帮助吗?
public async Task<IdentityResult> Create(ApplicationUser user, string password)
{
IdentityResult result = await _userManager.CreateAsync(user, password);
if (!result.Succeeded)
{
throw new AppException("Failed");
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
Create从控制器调用此函数:
[AllowAnonymous]
[HttpPost]
[Route("api/ApplicationUser/Register")]
public IActionResult Register([FromBody]ApplicationUserDto userDto)
{
//map dto to entity
var user = _mapper.Map<ApplicationUser>(userDto);
try
{
// save
_userService.Create(user, userDto.Password);
return Ok();
}
catch (AppException ex)
{
// return error message if there was an exception
return BadRequest(new { message = ex.Message });
} …Run Code Online (Sandbox Code Playgroud) asp.net-identity ×10
asp.net-mvc ×4
c# ×4
asp.net ×3
asp.net-core ×1
async-await ×1
facebook ×1
gmail ×1
signalr ×1
sql-server ×1
twitter ×1