标签: asp.net-identity

业务数据和身份的不同表或不同数据库

我通过Apress - Pro Asp.Net MVC 5和Identity Framework的免费章节阅读,现在,我想创建一个包含一些数据和身份的小型示例应用程序.

后来我想对Windows Azure进行测试部署.

现在,我应该为此应用程序创建一个单独的数据库,包含所有数据(产品,无论如何,IdentityData(用户帐户,Oauth链接...))还是创建两个数据库会更好?

我知道,如果我创建两个,我将能够为其他MVC应用程序使用相同的Identity-Data,但是对于MVC是否有某种最佳实践?

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

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

MVC从响应中删除ApplicationCookie

在之前使用表单身份验证的MVC项目中,我能够使用具有以下覆盖的动作过滤器从响应中剥离身份验证cookie.

public override void OnResultExecuted(ResultExecutedContext filterContext)
{
    filterContext.HttpContext.Response.Cookies.Remove(FormsAuthentication.FormsCookieName);
}
Run Code Online (Sandbox Code Playgroud)

我现在已经切换到使用基于OWIN的asp.net身份2.0,并且我想以同样的方式删除他们的身份验证cookie版本.

我修改了过滤器(下面)以使用新的cookie名称,但不再删除cookie.

public override void OnResultExecuted(ResultExecutedContext filterContext)
{
    const string authenticationCookie = CookieAuthenticationDefaults.CookiePrefix + DefaultAuthenticationTypes.ApplicationCookie;
    filterContext.HttpContext.Response.Cookies.Remove(authenticationCookie);
}
Run Code Online (Sandbox Code Playgroud)

有谁知道为什么?

authentication cookies asp.net-mvc owin asp.net-identity

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

尝试从Identity中删除角色

我试图从我的RoleManager中删除一个角色,我得到了错误

无法删除该对象,因为在ObjectStateManager中找不到该对象.

我的控制器

    [HttpPost]
    public void DeleteRole(string RoleName)
    {
        var RoleMan = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new ApplicationDbContext()));
        IdentityRole Role = new IdentityRole();
        Role.Name = RoleName;
        RoleMan.Delete(Role);
    }
Run Code Online (Sandbox Code Playgroud)

model-view-controller asp.net-mvc asp.net-mvc-4 asp.net-identity

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

通过Oauth2进行LinkedIn身份验证返回null结果(error = access_denied)

我将我的ASP.NET MVC Web应用程序从成员身份转移到身份验证,因为我无法再在LinkedIn上进行身份验证.

Facebook身份验证仍然正常,但LinkedIn总是在GetExternalLoginInfo调用后返回null loginInfo.

对于LinkedIn我正在使用Owin LinkedIn提供程序:用于.NET的LinkedIn API.我也试图跟随杰里·佩尔泽的这篇文章不成功.

Application调用执行ExecuteResult方法的ExternalLogin Action并回调ExternalLoginCallback(在我允许访问应用程序之后).如前所述,方法AuthenticationManager.GetExternalLoginInfoAsync()始终返回null loginInfo.

我检查了LinkedIn中的应用程序设置,一切似乎都没问题.

行动!我差点忘了说LinkedIn正在返回带有一般错误消息的URL:"GET/Account/ExternalLoginCallback?error = access_denied HTTP/1.1"

我可以使用DotNetOpenAuth.Clients(托管的github)进行身份验证,但我想使用Identity.

Startup.Auth.cs

var linkedInOptions = new LinkedInAuthenticationOptions();
        linkedInOptions.ClientId = "Xxxxx";
        linkedInOptions.ClientSecret = "Yyyyyyy";

        linkedInOptions.Scope.Add("r_fullprofile");

        linkedInOptions.Provider = new LinkedInAuthenticationProvider()
        {
            OnAuthenticated = async context =>
            {
                context.Identity.AddClaim(new System.Security.Claims.Claim("LinkedIn_AccessToken", context.AccessToken));
            }
        };

        linkedInOptions.SignInAsAuthenticationType = DefaultAuthenticationTypes.ExternalCookie;

        app.UseLinkedInAuthentication(linkedInOptions);
Run Code Online (Sandbox Code Playgroud)

ExternalLogin

public ActionResult ExternalLogin(string provider, string returnUrl)
    {
        // Request a redirect to the external login provider
        return new ChallengeResult(provider, Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl …
Run Code Online (Sandbox Code Playgroud)

authentication asp.net-mvc linkedin oauth-2.0 asp.net-identity

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

AuthenticationManager.SignIn上的HttpContext.Current.User.Identity

我正在使用ASP.Net Identity来管理我的用户.

在调用以下内容后我发现:

AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = isPersistent }, identity);
Run Code Online (Sandbox Code Playgroud)

当我检查用户当前是否通过HttpContext.Current.User.Identity.IsAuthenticated它进行身份验证返回false.

我设法true通过执行以下操作来获取此信息:

FormsAuthentication.SetAuthCookie(model.UserName, false);
Run Code Online (Sandbox Code Playgroud)

是否可以设置HttpContext.Current.User.Identity.IsAuthenticatedtrue

asp.net-mvc asp.net-identity

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

使用Web API的简单注入器2. PreApplicationStartMethod不运行

我正在关注我的Web API和AngularJS项目的Onion架构.在Infrastructure.DependencyResolution部分中,我使用的是Simple Injector.这是我的代码:

[assembly: PreApplicationStartMethod(typeof(IocConfig), "RegisterDependencies")]
namespace Infrastructure.DependencyResolution
{
    public class IocConfig
    {
         private static Container _container;

         public static void RegisterDependencies()
         {
              _container = new Container();

              _container.Verify();

              _container.RegisterWebApiRequest<IEntitiesContext>(() =>
              {
                   return new MyContext();
              });

              _container.RegisterWebApiRequest<IUserRepository, UserRepository>();
              _container.RegisterWebApiRequest<IAccountService, AccountService>();
              _container.RegisterWebApiRequest<IUnitOfWork, UnitOfWork>();

         }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,如果我尝试将url转到我的帐户控制器,我收到此错误:

尝试创建"AccountController"类型的控制器时发生错误.确保控制器具有无参数的公共构造函数.

我搜索并发现Simple Injector有一些不同的Web Api代码,如他们的网站所建议的那样:

// This is an extension method from the integration package.
container.RegisterWebApiControllers(GlobalConfiguration.Configuration);
Run Code Online (Sandbox Code Playgroud)

我也复制了它,但它不接受GlobalConfiguration.Configuration,我想是因为我在库项目中使用它.

当我在RegisterDependencies方法中设置断点时,Visual Studio不会在断点处停止.

有人能告诉我去哪儿的路吗?

.net onion-architecture simple-injector asp.net-web-api asp.net-identity

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

无法解析符号'Owin'

我目前正在尝试在AngularJS单页面应用程序中实现AspNet.Identity身份验证.奇怪的是,在我的ApplicationUserManager类中,我想使用Microsoft.Owin和Microsoft.AspNet.Identity.Owin但在这两种情况下我都得到一个"无法解析符号'Owin'"的错误消息.

有谁知道发生了什么?我已将依赖项包含在我的project.json中并下载了这些包.ApplicationUserManager类的简化版本是这样的:

using Matimez.Models;
using Matimez.Models.Users;
using Microsoft.AspNet.Identity.Owin;  //Error message
using Microsoft.Owin;                  //Error message
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;


public class ApplicationUserManager : UserManager<NewUser>
{ 
    public ApplicationUserManager(IUserStore<NewUser> store)
    : base(store)
        {
        }

public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
    var appDbContext = context.Get<MyAppContext>();
    var appUserManager = new ApplicationUserManager(new UserStore<NewUser>(appDbContext));

    return appUserManager;
}
}
Run Code Online (Sandbox Code Playgroud)

以下是我的project.json中的依赖项,如下所示:

  "dependencies": {
   "EntityFramework": "7.0.0-beta4",
   "EntityFramework.Commands": "7.0.0-beta4",
   "EntityFramework.SqlServer": "7.0.0-beta4",
   "MyApp.Models": "1.0.0-*",
   "MyApp.Services": "1.0.0-*",
   "Microsoft.AspNet.Diagnostics": "1.0.0-beta4",
   "Microsoft.AspNet.Diagnostics.Entity": "7.0.0-beta4",
   "Microsoft.AspNet.Identity": "3.0.0-beta4",
   "Microsoft.AspNet.Mvc": "6.0.0-beta4",
   "Microsoft.AspNet.Server.IIS": "1.0.0-beta4",
   "Microsoft.AspNet.Server.WebListener": "1.0.0-beta4",
   "Microsoft.AspNet.StaticFiles": "1.0.0-beta4", …
Run Code Online (Sandbox Code Playgroud)

c# owin asp.net-identity

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

测试请求是匿名的还是经过身份验证的WebAPI 2 +身份

我正在使用WebAPI 2 + ASP.NET Identity

在我的ApiController方法之一中,我想测试特定的HTTP请求是否来自经过身份验证的客户端(即,该请求是否包含授权标头)。

以下工作,但是也许有更好的方法?

private AuthContext db = new AuthContext();

// GET api/Orders/
[AllowAnonymous]
public async Task<IHttpActionResult> GetOrder(int id)
{
    // ApplicationUser is an IdentityUser.
    ApplicationUser currentUser = null;

    try
    {
        UserManager<ApplicationUser> userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));
        currentUser = await userManager.FindByNameAsync(User.Identity.GetUserName());
    }
    catch (Exception)
    {
    }

    if ( currentUser == null )
    {
        // Anonymous request.
        // etc...




    } else {
        // Authorized request.
        // etc...
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在使用默认的路由模板。另一个选择是将两种方法路由到授权请求和匿名请求(用适当的数据注释装饰)。

asp.net-web-api asp.net-web-api-routing asp.net-identity asp.net-web-api2

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

更新用户商店标识框架3.0

我几天来一直在寻找解决方案,只找到与旧版Identity Framework相关的答案.

项目使用c#Asp.net MVC 6 Entity Framework 7 beta8和Identity Framework 3.0.0-beta Code-First Data.在一个简单的项目中,我正在尝试实现仅限管理员的用户信息/编辑器页面,该页面允许通过访问UserStore或UserManager来编辑所有用户属性,并将更改保存到DBcontext中的标准Identity Framework用户存储中.

这是带有额外字段的ApplicationUser Model代码:

public class ApplicationUser : IdentityUser
{
    [Display(Name = "Last Name")]
    [DataType(DataType.Text)]
    [StringLength(50)]
    public string UserLastName { get; set; }

    [Display(Name = "First Name")]
    [DataType(DataType.Text)]
    [StringLength(50)]
    public string UserFirstName { get; set; }

    [Display(Name = "User")]
    public override string UserName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

Index视图的相应Controller代码:

public class UserListController : Controller
{
    private readonly UserManager<ApplicationUser> _userManager;
    private readonly SignInManager<ApplicationUser> _signInManager;

    public UserListController(UserManager<ApplicationUser> userManager, SignInManager<ApplicationUser> signInManager) …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc entity-framework asp.net-identity

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

如何在控制器中获取登录用户的ID?

使用Microsoft ASP.NET Identity,如何获取已在控制器中进行身份验证的用户的Id?

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

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