我已经完成了ASP.NET核心的配置文档.文档说您可以从应用程序的任何位置访问配置.
下面是模板创建的Startup.cs
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
if (env.IsEnvironment("Development"))
{
// This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately.
builder.AddApplicationInsightsSettings(developerMode: true);
}
builder.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container
public void ConfigureServices(IServiceCollection …Run Code Online (Sandbox Code Playgroud) 在以前的asp.net版本中,我们可以使用
@Request.Url.AbsoluteUri
Run Code Online (Sandbox Code Playgroud)
但它似乎已经改变了.我们怎样才能在asp.net core 1.0中做到这一点?
我试图重定向到ASP.NET MVC6中的另一个登录URL
我的帐户控制器登录方法有一个Route属性来更改网址.
[HttpGet]
[AllowAnonymous]
[Route("login")]
public IActionResult Login(string returnUrl = null)
{
this.ViewData["ReturnUrl"] = returnUrl;
return this.View();
}
Run Code Online (Sandbox Code Playgroud)
当我试图访问一个非正式的页面时,我被重定向到无效的网址,它应该只是
/login但是我得到了http://localhost/Account/Login?ReturnUrl=%2Fhome%2Findex
我已经配置了cookie身份验证路径,如下所示:
services.Configure<CookieAuthenticationOptions>(opt =>
{
opt.LoginPath = new PathString("/login");
});
Run Code Online (Sandbox Code Playgroud)
我添加了一个默认过滤器,以确保默认情况下所有URL都需要身份验证.
services.AddMvc(
options =>
{
options.Filters.Add(new AuthorizeFilter(new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build()));
});
Run Code Online (Sandbox Code Playgroud)
我已经检查过url /login确实加载了登录页面,而/account/login没有按预期加载.
编辑:我已按原样离开路线,(除了更改默认控制器和操作)
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Site}/{action=Site}/{id?}");
});
Run Code Online (Sandbox Code Playgroud) 在ASP.NET Core应用程序中,我想创建某些角色作为管理不同用户权限的基础.遗憾的是,文档告知详细说明如何使用自定义角色,例如在控制器/操作中,而不是如何创建它们.我发现我可以使用RoleManager<IdentityRole>此实例,当实例在应用程序中注册其定义的和ASP.NET Core身份时,会自动注入到控制器构造函数中.
这让我添加一个这样的自定义角色:
var testRole = new IdentityRole("TestRole");
if(!roleManager.RoleExistsAsync(testRole.Name).Result) {
roleManager.CreateAsync(testRole);
}
Run Code Online (Sandbox Code Playgroud)
它在数据库中工作并创建角色.但是这种检查总是会在数据库上产生开销,调用特定的控制器/动作.所以我想在我的应用程序启动后检查一次,如果自定义角色存在并添加它们.ConfigureServicesStartup.cs中的方法似乎很适合.
但是:如何创建RoleManager<IdentityRole>类的实例呢?我想在这里使用最佳实践方法,而不是通过自己创建依赖实例来解决这个问题,这似乎会导致很多工作,因为它没有很好的文档化,并且肯定不会遵循最佳实践,因为ASP.NET Core正在使用依赖注入这样的事情(这在我的意见中也是合理的).
换句话说:我需要在控制器之外使用依赖注入.
在MVC5 Identity 2中,SignInManager.PasswordSignInAsync获取登录用户名.
var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
Run Code Online (Sandbox Code Playgroud)
但我的用户名和电子邮件不一样.但我想通过电子邮件地址登录.所以我该怎么做?谢谢
asp.net-mvc identity asp.net-mvc-5 asp.net-identity asp.net-identity-2
我先用EF 6代码创建了我的数据层,然后通过继承Seed的EvInitializer类方法填充db DropCreateDatabaseIfModelChanges.Seed方法的实现是
protected override void Seed(EvContext context)
{
//Add other entities using context methods
ApplicationUserManager manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context));
var user = new ApplicationUser { Email = "admin@myemail.com" ,UserName = "admin@myemail.com"};
var result = await manager.CreateAsync(user, "Temp_123");//this line gives error. obviously await cannot be used in non- async method and I cannot make Seed async
}
Run Code Online (Sandbox Code Playgroud)
我的问题是我如何Seed使用UserManager类在方法中添加用户.当我更改
var result = awit manager.CreateAsync(user, "Temp_123");
为
var result = …
entity-framework entity-framework-6 asp.net-identity asp.net-identity-2
嗨,伙计们我正在使用bootstrapcarousal并且图像高度和宽度有问题.即使我在img属性中定义它仍然显示为原始分辨率,以下是我正在使用的代码
<div class="col-md-6 col-sm-6">
<div id="myCarousel" class="carousel slide">
<div class="carousel-inner">
<div class="item active">
<img src="http://placehold.it/650x450/aaa&text=Item 3" height="300" width="300" />
</div>
<div class="item">
<img src="http://placehold.it/350x350/aaa&text=Item 3" height="300" width="300" />
</div>
<div class="item">
<img src="http://placehold.it/350x350/aaa&text=Item 3" height="300" width="300" />
</div>
</div>
<!-- Controls -->
<a class="left carousel-control" href="#myCarousel" data-slide="prev">
<span class="icon-prev"></span>
</a>
<a class="right carousel-control" href="#myCarousel" data-slide="next">
<span class="icon-next"></span>
</a>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
这是演示.
无论原始分辨率如何,我该怎么做才能以一定的高度和宽度填充图像.
这是我的输出:http://jsbin.com/zuxipa/1/
基本上,我希望行内的第二个div更小(它目前在col-md-1).我怎么能这样做?
使用VS 2013,标准MVC模板和身份提供程序框架
用户已登录,我有:
//....
UserManager.AddToRole(User.Identity.GetUserId(), "Members"); # Line X
RedirectToAction("Index", "Members");
Run Code Online (Sandbox Code Playgroud)
会员控制员如下:
[Authorize(Roles="Members")]
public class MembersController : Controller
{
// GET: Members
public ActionResult Index()
{
return View();
}
}
Run Code Online (Sandbox Code Playgroud)
执行第X行后,我可以确认用户已添加到表中dbo.AspNetUserRoles.但是,用户在到达成员控制器时未通过角色检查. User.IsInRole("Members")返回false.
如果用户注销然后再次登录,则将访问"成员"控制器,即User.IsInRole("Members")现在返回true.
有缓存吗?为何延误?我该如何克服它?
我也尝试将第X行的方法转换为异步方法并使用UserManager.AddToRoleAsync.同样的延迟效应仍然存在.
在ASP.Net MVC 5,ApplicationUser可以扩展为具有自定义属性.我已经扩展它,现在它有一个新属性叫做DisplayName:
// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
public class ApplicationUser : IdentityUser {
public string ConfirmationToken { get; set; }
public string DisplayName { get; set; } //here it is!
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager) {
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add …Run Code Online (Sandbox Code Playgroud) asp.net-core ×4
c# ×4
asp.net-mvc ×3
asp.net ×2
carousel ×1
css ×1
html ×1
identity ×1
roleprovider ×1
user-roles ×1