小编tmg*_*tmg的帖子

如何在ASP.NET Core中的任何类中访问Configuration?

我已经完成了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)

c# asp.net-core-mvc asp.net-core

102
推荐指数
7
解决办法
8万
查看次数

如何在asp.net core 1.0中获取当前url

在以前的asp.net版本中,我们可以使用

@Request.Url.AbsoluteUri
Run Code Online (Sandbox Code Playgroud)

但它似乎已经改变了.我们怎样才能在asp.net core 1.0中做到这一点?

asp.net-core-mvc asp.net-core asp.net-core-1.0

89
推荐指数
10
解决办法
9万
查看次数

ASP.NET核心,更改默认重定向未授权

我试图重定向到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)

c# asp.net asp.net-identity asp.net-core-mvc asp.net-core

27
推荐指数
6
解决办法
3万
查看次数

ASP.NET Core Identity在应用程序启动时添加自定义用户角色

在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正在使用依赖注入这样的事情(这在我的意见中也是合理的).

换句话说:我需要在控制器之外使用依赖注入.

c# user-roles asp.net-core-mvc asp.net-core

20
推荐指数
2
解决办法
1万
查看次数

如何使用身份2中的电子邮件登录?

在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

16
推荐指数
1
解决办法
1万
查看次数

在Db Initializer的Seed方法中创建Asp.net Identity用户

我先用EF 6代码创建了我的数据层,然后通过继承SeedEvInitializer类方法填充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

16
推荐指数
2
解决办法
1万
查看次数

在Bootstrap轮播中拉伸和填充图像

嗨,伙计们我正在使用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)

这是演示.

无论原始分辨率如何,我该怎么做才能以一定的高度和宽度填充图像.

html css carousel twitter-bootstrap twitter-bootstrap-3

13
推荐指数
4
解决办法
3万
查看次数

如何使列大小小于col-xx-1

这是我的输出:http://jsbin.com/zuxipa/1/

基本上,我希望行内的第二个div更小(它目前在col-md-1).我怎么能这样做?

twitter-bootstrap twitter-bootstrap-3

9
推荐指数
2
解决办法
1万
查看次数

在ASP.NET标识框架中更改时刷新当前用户的角色?

使用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 roleprovider asp.net-identity

8
推荐指数
1
解决办法
4742
查看次数

如何在ASP.Net MVC 5视图中获取ApplicationUser的自定义属性值?

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)

c# asp.net asp.net-mvc asp.net-mvc-5 asp.net-identity

8
推荐指数
1
解决办法
3222
查看次数