在asp net core中设置身份验证(asp net 5)

时间:2016-07-21 06:57:21

标签: asp.net asp.net-mvc asp.net-mvc-5

我需要能够从测试中的代码中验证我的用户,就像这样

var identity = new GenericIdentity("ausov@fsb.ru", "UserId");
Thread.CurrentPrincipal = new GenericPrincipal(identity, null);

或者像这样

FormsAuthentication.SetAuthCookie(username, rememberMe);

但是这些方法在asp net 5中不起作用。

1 个答案:

答案 0 :(得分:6)

要在ASP.NET Core中使用它,首先,使用NuGet添加Microsoft.AspNetCore.Authentication.Cookies

在Startup.cs中,将其添加到Configure方法:

dt %>%
    group_by(name) %>%
    mutate(quantlabel = as.character(cut(value, breaks = quantile(value, probs = seq(0,1,.25)), include.lowest = T)))

Source: local data frame [17 x 3]
Groups: name [3]

     name value quantlabel
   <fctr> <int>      <chr>
1       a     1   [1,2.75]
2       a     2   [1,2.75]
3       a     3 (2.75,4.5]
4       a     4 (2.75,4.5]
5       a     5 (4.5,6.25]
6       a     6 (4.5,6.25]
7       a     7   (6.25,8]
8       a     8   (6.25,8]
9       b     1   [1,1.75]
10      b     2 (1.75,2.5]
11      b     3 (2.5,3.25]
12      b     4   (3.25,4]
13      c     1      [1,2]
14      c     2      [1,2]
15      c     3      (2,3]
16      c     4      (3,4]
17      c     5      (4,5]

然后将其命名为登录:

  app.UseCookieAuthentication(new CookieAuthenticationOptions()
  {
    AuthenticationScheme = "PutANameHere",
    LoginPath = new PathString("/Account/Login/"),
    AutomaticAuthenticate = true,
    AutomaticChallenge = true
  });

有关详细信息,请查看以下内容: https://docs.asp.net/en/latest/security/authentication/cookie.html