标签: asp.net-identity

AspNet Core脚手架应用中的登录和注册页面在哪里?

在VS 2017中,我创建了一个新的ASP.NET核心Web应用程序.在向导的第二页上,我选择了Web应用程序,对于身份验证,我选择了"个人用户帐户".

现在,我正在尝试找到与/ Account/Register和/ Account/Login相关联的页面.

_Layout.cshtml引入_LoginPartial.cshtml,就像在经典MVC中一样:

<div class="navbar-collapse collapse">
    <ul class="nav navbar-nav">
        <li><a asp-page="/Index">Home</a></li>
        <li><a asp-page="/About">About</a></li>
        <li><a asp-page="/Contact">Contact</a></li>
    </ul>
    <partial name="_LoginPartial" />
</div>
Run Code Online (Sandbox Code Playgroud)

如果用户未登录,则_LoginPartial包含<a>指向登录和注册页面的标记:

<ul class="nav navbar-nav navbar-right">
    <li><a asp-area="Identity" asp-page="/Account/Register">Register</a></li>
    <li><a asp-area="Identity" asp-page="/Account/Login">Login</a></li>
</ul>
Run Code Online (Sandbox Code Playgroud)

这一切似乎都有道理.但我希望Areas文件夹结构包含Register和Login文件夹.它不是.我发现的唯一的东西是_ViewStart.cshtml

区域文件结构

我知道脚手架代码有效,当我运行项目时,Register链接指向"/ Identity/Account/Register",Login链接指向"/ Identity/Account/Login".点击注册链接可以获得一个注册页面,其中包含"创建新帐户"文本.

但我无法在项目的任何地方找到"创建新帐户"文本.

有人能告诉我我错过了什么吗?

asp.net-identity asp.net-core razor-pages

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

OWIN安全 - 如何实施OAuth2刷新令牌

我使用Visual Studio 2013附带的Web Api 2模板有一些OWIN中间件来进行用户身份验证等.

OAuthAuthorizationServerOptions我注意到,OAuth2服务器设置为分发在14天后到期的令牌

 OAuthOptions = new OAuthAuthorizationServerOptions
 {
      TokenEndpointPath = new PathString("/api/token"),
      Provider = new ApplicationOAuthProvider(PublicClientId,UserManagerFactory) ,
      AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
      AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
      AllowInsecureHttp = true
 };
Run Code Online (Sandbox Code Playgroud)

这不适合我的最新项目.我想分发可以使用a刷新的短暂bearer_tokensrefresh_token

我做了很多谷歌搜索,找不到任何有用的东西.

所以这就是我设法得到的.我现在已经达到了"WTF do I now"的地步.

我写了一个根据类的属性RefreshTokenProvider实现:IAuthenticationTokenProviderRefreshTokenProviderOAuthAuthorizationServerOptions

    public class SimpleRefreshTokenProvider : IAuthenticationTokenProvider
    {
       private static ConcurrentDictionary<string, AuthenticationTicket> _refreshTokens = new ConcurrentDictionary<string, AuthenticationTicket>();

        public async Task CreateAsync(AuthenticationTokenCreateContext context)
        {
            var guid = Guid.NewGuid().ToString();


            _refreshTokens.TryAdd(guid, context.Ticket);

            // hash??
            context.SetToken(guid);
        }

        public async Task …
Run Code Online (Sandbox Code Playgroud)

c# oauth-2.0 asp.net-web-api owin asp.net-identity

76
推荐指数
3
解决办法
11万
查看次数

使用OWIN标识从多个API客户端注册Web API 2外部登录

我想要以下架构(我已经为这个例子编写了产品名称):

在一台服务器 http://api.prettypictures.com 上运行的Web API 2应用程序

MVC 5客户端应用程序在另一台服务器上运行 http://www.webpics.com

我希望www.webpics.com客户端应用程序使用Pretty Pictures API:

  • 使用用户名和密码注册新帐户
  • 在Facebook/Google/Twitter/Microsoft注册新帐户
  • 登录
  • 检索图片

所有上述工作除了在Facebook,Google等注册外部账户外.

我无法确定从API的单独客户端用户创建外部帐户的正确流程.

我已经研究了认证流程中可用的大多数文档,如下所示: 在此输入图像描述

我已经在OWIN的新身份模型上阅读了我所能做的一切.

我已经在Visual Studio 2013中检查了SPA模板.它演示了如何完成我需要的大部分工作,但仅限于客户端和API在同一主机上时; 如果我希望多个客户端访问我的API并且能够让用户通过Google等注册,那么它就无法正常工作,而且我可以告诉OWIN身份验证流程中断.

到目前为止流程如下:

  • 用户浏览www.webpics.com/Login
  • www.webpics.com调用api.prettypictures.com/Account/ExternalLogins(用RETURNURL设置回去在回调www.webpics.com),并显示最终链接到用户
  • 用户点击"Google"
  • 浏览器使用提供者的名称重定向到api.prettypictures.com/Account/ExternalLogin等.
  • API的ExternalLogin操作实例化对google.com的挑战
  • 浏览器被重定向到google.com
  • 用户输入用户名和密码(如果他们尚未登录google.com)
  • google.com现在提供安全许可:"api.prettypictures.com"想要访问您的电子邮件地址,姓名,妻子,孩子等.这样可以吗?
  • 用户点击"是"并使用Google设置的Cookie将其带回api.prettypictures.com/Account/ExternalLogin.

这是我被卡住的地方.接下来应该发生的是以某种方式通知客户端应用程序用户已成功通过google.com进行身份验证,并获得一次性访问代码以便稍后交换访问令牌.如有必要,客户端应用程序应该有机会提示用户输入与google.com登录相关联的用户名.

我不知道如何促进这一点.

实际上,此时浏览器在谷歌回调后最终坐在api.prettypictures.com/Account/ExternalLogin端点上.该API已登录Google,但客户端不知道如何处理该问题.我应该将该饼干送回www.webpics.com吗?

在SPA应用程序中,它通过AJAX完成,google.com将返回一个令牌作为URL片段,它一切运行良好,因为它都在一个域上.但这远远超过了拥有多个客户可以充分利用的"API"的重要性.

救命!

asp.net-mvc oauth-2.0 owin asp.net-identity asp.net-web-api2

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

如何在ASP.NET MVC 5中实现自定义身份验证

我正在开发一个ASP.NET MVC 5应用程序.我有一个现有的数据库,我从中创建了我的ADO.NET实体数据模型.我在该DB中有一个包含"username"和"password"列的表,我想用它们在我的Webapp中实现身份验证和授权; 我无法创建任何其他数据库或表或列,因为客户的要求,我无法使用标准身份验证.我不需要管理注册,密码更改或其他内容:只需使用密码和用户名登录.我怎样才能做到这一点?

c# authentication asp.net-mvc authorization asp.net-identity

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

获取ASP.NET Identity 2.0中的当前用户标识

我刚刚切换到使用Identity 2.0的新2.0版本.在1.0中我可以使用获取用户对象manager.FindByIdAsync(User.Identity.GetUserId()).该GetUserId()方法似乎不存在于2.0中.

现在我可以弄清楚的是使用manager.FindByEmailAsync(User.Identity.Name)哪些引用users表中的用户名字段.在我的应用程序中,它设置为与电子邮件字段相同.

当有人需要更新他们的电子邮件时,我可以看到这会导致问题.有没有办法根据Identity 2.0 Framework中不变的值(例如id字段)获取当前登录的用户对象?

c# asp.net asp.net-identity

73
推荐指数
5
解决办法
10万
查看次数

如何在ASP.NET 5 MVC 6(vNext)中定义Identity的密码规则?

默认情况下,ASP.NET 5中提供的默认身份提供程序具有非常严格的密码规则,需要小写字符,大写字符,非字母数字字符和数字.我正在寻找一种方法来改变提供商的密码要求.

以前在ASP.NET 4中,可以通过Web.config XML文件配置提供程序,如前所述.但是,ASP.NET 5使用基于新代码的配置模式,并且不清楚如何配置身份.

如何更改应用程序的密码要求?

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

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

更新用户数据 - ASP.NET标识

我已经在ApplicationUser课程中添加了自定义字段,
我还创建了一个表单,用户可以通过该表单输入/编辑字段.
但由于某种原因,我无法更新数据库中的字段.

[HttpPost]
[ActionName("Edit")]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Manage(EditProfileViewModel model)
{
    if (ModelState.IsValid)
    {
        // Get the current application user
        var user = User.Identity.GetApplicationUser();

        // Update the details
        user.Name = new Name { First = model.FirstName, Last = model.LastName, Nickname = model.NickName };
        user.Birthday = model.Birthdate;

        // This is the part that doesn't work
        var result = await UserManager.UpdateAsync(user);

        // However, it always succeeds inspite of not updating the database
        if (!result.Succeeded)
        {
            AddErrors(result);
        }
    }

    return …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc asp.net-mvc-5 asp.net-identity

69
推荐指数
4
解决办法
9万
查看次数

ASP.NET MVC 5是否与WebMatrix SimpleMembershipProvider不兼容?

我们有一个基于ASP.NET MVC 4和Web API的现有应用程序.该网站的管理部分使用简单成员资格.我有兴趣将应用程序升级到MVC 5/Web API 2,以利用已添加的一些新功能.但看起来它们可能不兼容.

具体来说,在将NuGet中的RC包安装到我的解决方案中的一个项目中并更新web.config信息之后,应用程序在启动期间开始在调用的行上死亡WebSecurity.InitializeDatabaseConnection(),但有以下异常:

[MethodAccessException: Attempt by security transparent method 'WebMatrix.WebData.PreApplicationStartCode.OnConnectionOpened(System.Object, WebMatrix.Data.ConnectionEventArgs)' to access security critical method 'System.Web.WebPages.HttpContextExtensions.RegisterForDispose(System.Web.HttpContextBase, System.IDisposable)' failed.]
   WebMatrix.WebData.PreApplicationStartCode.OnConnectionOpened(Object sender, ConnectionEventArgs e) +70
   WebMatrix.Data.Database.OnConnectionOpened() +70
   WebMatrix.Data.Database.EnsureConnectionOpen() +51
   WebMatrix.Data.Database.QueryValue(String commandText, Object[] args) +63
   WebMatrix.WebData.DatabaseWrapper.QueryValue(String commandText, Object[] parameters) +13
   WebMatrix.WebData.SimpleMembershipProvider.GetUserId(IDatabase db, String userTableName, String userNameColumn, String userIdColumn, String userName) +206
   WebMatrix.WebData.SimpleMembershipProvider.ValidateUserTable() +87
Run Code Online (Sandbox Code Playgroud)

使用我尚未升级的简单成员资格的同一解决方案中的其他项目继续正常工作.

当然,谷歌搜索更多信息会引发大量的点击,但WebMatrix没什么特别的.

FWIW:我知道Microsoft已经引入了(又一个)成员身份和身份解决方案,但除非有一种方法可以将其用于现有的简单成员资格表,或者是我们所有现有用户数据的无缝迁移路径,这不是一个真正的选择为了我们.

更新(10月11日)

我刚刚尝试了一下我们应用程序当前主干的新结帐.我正在使用Visual Studio 2012,但是按照MS的说明升级现有项目.更新到MVC 5/Web API 2/EF 6后,该应用程序启动运行就好了.

web.config删除中没有明确的信任要求.我加入了代码这个问题Global.asax.cs …

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

68
推荐指数
2
解决办法
4万
查看次数

无法从apicontroller中的OwinContext获取UserManager

我正在关注Microsoft示例以使用Identity 2.0.0实现电子邮件验证

我被困在这一部分

public ApplicationUserManager UserManager
{
   get
   {
      return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
   }
   private set
   {
      _userManager = value;
   }
}
Run Code Online (Sandbox Code Playgroud)

这适用于controllerHttpContext不包含ApiController中的任何GetOwinContext方法.

所以我试过HttpContext.Current.GetOwinContext()但方法GetUserManager不存在.

我无法找到一种方法来获得UserManager我在Startup.Auth.cs中的构建

// For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
public void ConfigureAuth(IAppBuilder app)
{
    //Configure the db context, user manager and role manager to use a single instance per request
    app.CreatePerOwinContext(ApplicationDbContext.Create);
    app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create); 
    ...
}
Run Code Online (Sandbox Code Playgroud)

这条线

app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
Run Code Online (Sandbox Code Playgroud)

调用以下函数来配置 UserManager …

c# asp.net-web-api owin asp.net-identity

68
推荐指数
3
解决办法
7万
查看次数

如何在Microsoft.AspNet.Identity.EntityFramework.IdentityUser中更改id的类型

(ASP.NET MVC 5,EF6,VS2013)

我试图弄清楚如何在类型中将"Id"字段的类型从字符串更改为int:

Microsoft.AspNet.Identity.EntityFramework.IdentityUser
Run Code Online (Sandbox Code Playgroud)

为了使新用户帐户与整数ID而不是GUID相关联.但似乎这比在我的派生用户类中简单地添加类型为int的新Id属性更复杂.看看这个方法签名:

(来自程序集Microsoft.AspNet.Identity.Core.dll)

public class UserManager<TUser> : IDisposable where TUser : global::Microsoft.AspNet.Identity.IUser
  {
  ...
  public virtual Task<IdentityResult> AddLoginAsync(string userId, UserLoginInfo login);
  ...
  }
Run Code Online (Sandbox Code Playgroud)

因此,似乎ASP.NET身份框架中还有其他方法要求userId为字符串.我还需要重新实现这些课程吗?

解释为什么我不想在用户表中存储id的GUID:

- 将有其他表通过外键将数据与users表相关联.(当用户在网站上保存内容时.)我认为没有理由使用较大的字段类型并花费额外的数据库空间而没有明显的优势.(我知道还有其他关于使用GUID和int ID的帖子,但似乎有很多人建议int id更快并且使用更少的空间,这仍然让我感到疑惑.)

- 我计划公开一个restful端点,以允许用户检索有关特定用户的数据.我认为:

/users/123/name
Run Code Online (Sandbox Code Playgroud)

比...更干净

/users/{af54c891-69ba-4ddf-8cb6-00d368e58d77}/name
Run Code Online (Sandbox Code Playgroud)

有谁知道为什么ASP.NET团队决定以这种方式实现ID?在尝试将其更改为int类型时,我是否只是短视?(也许我缺少一些好处.)

谢谢...

-ben

asp.net-mvc entity-framework owin asp.net-mvc-5 asp.net-identity

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