ASP.NET WebApi按标记登录如何区分返回状态代码?

时间:2016-12-08 22:04:32

标签: asp.net asp.net-web-api owin

我想为try catch块返回500状态代码 但总是返回400个状态码 如果电子邮件和密码错误,我想显示400状态代码和错误500状态代码。

这是我的代码。请帮帮我。

    public override Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
    {
        return Task.Factory.StartNew(() =>
        {

            try
            {

                context.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "http://localhost:36725" });

                string usertype = context.OwinContext.Get<string>("usertype");


                if (usertype == "Profile")
                {
                    var username = context.UserName;
                    var password = context.Password;

                    var profiles = new Profiles();

                    Profile profile = profiles.Login(username, password);

                    if (profile != null)
                    {

                        var claims = new List<Claim>()
                          {
                            new Claim("ID", profile.ID.ToString()),
                            new Claim(ClaimTypes.Name, profile.Name),
                            new Claim(ClaimTypes.Surname, profile.Surname),
                            new Claim("ProfilePhotoUrl", profile.ProfilePhotoUrl),
                            new Claim("UserName", profile.UserName),
                            new Claim(ClaimTypes.Role, profile.UserType.Name),
                            new Claim("Language", profile.Language.Name)
                           };

                        ClaimsIdentity oAutIdentity = new ClaimsIdentity(claims, Startup.OAuthOptions.AuthenticationType);

                        context.Validated(new AuthenticationTicket(oAutIdentity, new AuthenticationProperties() { }));

                    }
                    else
                    {
                        context.SetError("invalid_grant", "The e-mail or password is incorrect");
                    }
                }
                else if (usertype == "Page")
                {
                    var username = context.UserName;
                    var password = context.Password;

                    var pages = new Pages();

                    Page page = pages.Login(username, password);

                    if (page != null)
                    {

                        var claims = new List<Claim>()
                            {
                                new Claim("ID", page .ID.ToString()),
                                new Claim(ClaimTypes.Name, page.Name),
                                new Claim("ProfilePhotoUrl", page.ProfilePhotoUrl),
                                new Claim("UserName", page.UserName),
                                new Claim(ClaimTypes.Role, page.UserType.Name)
                            };

                        ClaimsIdentity oAutIdentity = new ClaimsIdentity(claims, Startup.OAuthOptions.AuthenticationType);

                        context.Validated(new AuthenticationTicket(oAutIdentity, new AuthenticationProperties() { }));

                    }
                    else
                    {
                        context.SetError("invalid_grant", "The e-mail or password is incorrect");

                    }
                }
                else if (usertype == "Anonymous")
                {

                    var username = context.UserName;

                    var password = context.Password;

                    string name = context.OwinContext.Get<string>("name");

                    string surname = context.OwinContext.Get<string>("surname");


                    var profiles = new Profiles();

                    Profile profile = profiles.Login(name, surname, username, password);

                    if (profile != null)
                    {

                        var claims = new List<Claim>()
                          {
                            new Claim("ID", profile.ID.ToString()),
                            new Claim(ClaimTypes.Name, profile.Name),
                            new Claim(ClaimTypes.Surname, profile.Surname),
                            new Claim(ClaimTypes.Email, profile.Email),
                            new Claim(ClaimTypes.Role, profile.UserType.Name),
                           };

                        ClaimsIdentity oAutIdentity = new ClaimsIdentity(claims, Startup.OAuthOptions.AuthenticationType);

                        context.Validated(new AuthenticationTicket(oAutIdentity, new AuthenticationProperties() { }));

                    }
                    else
                    {
                        Http.Log log = new Http.Log("An unknown error occurred");
                        context.SetError("invalid_grant", "An unknown error occurred");
                    }
                }
                else
                {

                    Http.Log log = new Http.Log("User Type is incorrect");
                    context.SetError("invalid_grant", "User Type is incorrect");
                }

            }
            catch (Exception ex)
            {

                Http.Log log = new Http.Log(ex.Message + " " + "An unknown error occurred");
                context.SetError("invalid_grant", "An unknown error occurred");

            }
        });
    }

1 个答案:

答案 0 :(得分:0)

这可以通过使用适当的HttpResponseException

投放HttpStatusCode来实现
if (EmailAndOrPasswordIsWrong)
{
    throw new HttpResponseException(HttpStatusCode.BadRequest); // 400
}

if (SomethingElseGoesWrong)
{
    throw new HttpResponseException(HttpStatusCode.InternalServerError); // 500
}