Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET MVC基于索赔的用户;“外部”;认证 背景_C#_Asp.net Mvc_Owin_Claims Based Identity_Custom Authentication - Fatal编程技术网

C# ASP.NET MVC基于索赔的用户;“外部”;认证 背景

C# ASP.NET MVC基于索赔的用户;“外部”;认证 背景,c#,asp.net-mvc,owin,claims-based-identity,custom-authentication,C#,Asp.net Mvc,Owin,Claims Based Identity,Custom Authentication,我们的web应用程序使用外部身份验证,从某种意义上说,用户的用户名/密码不是在本地验证的,而是在中央单一登录类型网站的web应用程序“外部”验证的。通过本地服务器变量(HTTP\u EMPLOYEEID等)可以获得身份验证证明(和用户身份)。然而,它不像针对Google、Facebook或其他基于OAuth的设置进行身份验证那样完全是外部的。所以我只是想区分一下,这样就不会与ASP.NET Identity/Owin中的“外部登录”一词相冲突 问题 我试图找出一种干净的方法来利用经过身份验证的用

我们的web应用程序使用外部身份验证,从某种意义上说,用户的用户名/密码不是在本地验证的,而是在中央单一登录类型网站的web应用程序“外部”验证的。通过本地服务器变量(
HTTP\u EMPLOYEEID
等)可以获得身份验证证明(和用户身份)。然而,它不像针对Google、Facebook或其他基于OAuth的设置进行身份验证那样完全是外部的。所以我只是想区分一下,这样就不会与ASP.NET Identity/Owin中的“外部登录”一词相冲突

问题 我试图找出一种干净的方法来利用经过身份验证的用户数据(来自服务器变量)并将其传递给ASP.NET身份验证。但是,在用户登录到应用程序之前,必须根据web服务查找用户配置文件和角色数据

我想使用Owin和基于声明的标识,但我不确定是否也应该使用ASP.NET标识,或者只是对声明进行更“纯粹”的实现。我喜欢不重新发明轮子的想法,但如果从web服务识别和查找用户的方式不适合典型的ASP.NET身份使用,我也不想把方形的销钉塞进圆形的孔中(俗话说)

例如,如果我采取更纯粹的方法,我可以做如下事情:

// Get the current user's id
var userId = HttpContext.Current.Request.ServerVariables["HTTP_EMPLOYEEID"];

// Get profile and role data from a web service
MyUser user = MyUserService.GetUserById(userId);

// Create claims
var claims = new Claim[]
{
    new Claim(ClaimTypes.Name, user.Id),
    new Claim(ClaimTypes.Email, user.Email),
    new Claim(ClaimTypes.Role, user.Role), // there can be more roles, but you get the idea
    // etc.
};

// Establish identity and login
var identity = new ClaimsIdentity(claims, "CookieAuthentication");
HttpContext.Current.GetOwinContext().Authentication.SignIn(identity);
但我也知道我可以使用ASP.NET标识(只是没有实体框架的东西),只需实现IUser、IUserStore、IRoleStore(以及其他最低要求的东西),并使用Microsoft现有的、已建立的框架来处理此问题。争论的焦点是,这更符合当前的标准,并且可能更容易扩展到其他类型的身份验证(例如,如果本地用户名/密码或Google/Facebook最终成为当前基于ServerVariables的设置之外的其他允许的身份验证选项)

以前走过这条路的人有什么建议吗?我应该将服务器变量注入的数据视为自定义中间件,并通过ASP.NET Identity利用它,还是不必担心它在那个世界中的位置,而是采用上述更“纯粹”的方法


p、 我使用的是ASP.NET 4.6.1,而不是新的ASP.NET内核。

我也有类似的饱和度。我不想使用整个ASP.Net标识,因为我需要再次验证用户身份—我们的外部身份验证

所以我只使用OWIN声明身份验证,它基本上创建了带有声明的身份验证cookie;类似于我们以前使用的表单身份验证


谢谢是的,这就是我用上面描述的“纯”索赔方法思考的方向。我只是好奇,因为涉及到另一个用户配置文件/角色数据查找,可能ASP.NET标识值得考虑,因为据推测,它是非常可自定义的。一旦创建声明,在用户注销或关闭浏览器之前,它将不需要再次访问外部源。此外,默认属性也可以识别它。
public class OwinAuthenticationService 
{
    private readonly HttpContextBase _context;
    private const string AuthenticationType = "ApplicationCookie";

    public OwinAuthenticationService(HttpContextBase context)
    {
        _context = context;
    }

    public void SignIn(User user)
    {
        IList<Claim> claims = new List<Claim>
        {
            new Claim(ClaimTypes.Sid, user.Id.ToString()),
            new Claim(ClaimTypes.Name, user.UserName),
            new Claim(ClaimTypes.GivenName, user.FirstName),
            new Claim(ClaimTypes.Surname, user.LastName),
        };

        foreach (Role role in user.Roles)
        {
            claims.Add(new Claim(ClaimTypes.Role, role.Name));
        }

        ClaimsIdentity identity = new ClaimsIdentity(claims, AuthenticationType);

        IOwinContext context = _context.Request.GetOwinContext();
        IAuthenticationManager authenticationManager = context.Authentication;

        authenticationManager.SignIn(identity);
    }

    public void SignOut()
    {
        IOwinContext context = _context.Request.GetOwinContext();
        IAuthenticationManager authenticationManager = context.Authentication;

        authenticationManager.SignOut(AuthenticationType);
    }
}
public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = "ApplicationCookie",
            LoginPath = new PathString("/Account/Login"),
            Provider = new CookieAuthenticationProvider
            {
                OnApplyRedirect = ctx =>
                {
                    if (!IsApiRequest(ctx.Request))
                    {
                        ctx.Response.Redirect(ctx.RedirectUri);
                    }
                }
            }
        });
    }

    private static bool IsApiRequest(IOwinRequest request)
    {
        string apiPath = VirtualPathUtility.ToAbsolute("~/api/");
        return request.Uri.LocalPath.ToLower().StartsWith(apiPath);
    }
}